Skip to content

Commit 4a3eb6f

Browse files
Ravi kumarRavi kumar
Ravi kumar
authored and
Ravi kumar
committed
quite a few programs
1 parent df76250 commit 4a3eb6f

39 files changed

+1123
-109
lines changed

DisectingString/.vscode/.BROWSE.VC.DB

220 KB
Binary file not shown.

DisectingString/.vscode/launch.json

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "C++ Launch",
6+
"type": "cppdbg",
7+
"request": "launch",
8+
"program": "enter program name, for example ${workspaceRoot}/a.out",
9+
"args": [],
10+
"stopAtEntry": false,
11+
"cwd": "${workspaceRoot}",
12+
"environment": [],
13+
"externalConsole": true,
14+
"linux": {
15+
"MIMode": "gdb"
16+
},
17+
"osx": {
18+
"MIMode": "lldb"
19+
},
20+
"windows": {
21+
"MIMode": "gdb"
22+
}
23+
},
24+
{
25+
"name": "C++ Attach",
26+
"type": "cppdbg",
27+
"request": "attach",
28+
"program": "enter program name, for example ${workspaceRoot}/a.out",
29+
"processId": "${command.pickProcess}",
30+
"linux": {
31+
"MIMode": "gdb"
32+
},
33+
"osx": {
34+
"MIMode": "lldb"
35+
},
36+
"windows": {
37+
"MIMode": "gdb"
38+
}
39+
}
40+
]
41+
}

DisectingString/DisectingString.c

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include<stdio.h>
2+
#include<string.h>
3+
4+
int dealWithSpace(int );
5+
int dealWithQuotes(int );
6+
//void dealWithLastWord(void);
7+
8+
char inputString[20];
9+
int flag1=0, quoteFlag1 = 0;
10+
11+
int main(void){
12+
int length;
13+
int i=0, j=0;
14+
15+
printf("Enter string: ");
16+
fgets(inputString, 20, stdin);
17+
18+
19+
20+
// printf("the inputString is: %s", inputString);
21+
22+
length = (int)strlen(inputString);
23+
// printf("%d", length);
24+
25+
while(i < length-2) {
26+
if( (inputString[i] == (char)32 || inputString[i] == (char)'\0') && ( inputString[i+1] != (char)34) )
27+
i = dealWithSpace(i);
28+
else if(inputString[i] == (char)34)
29+
i = dealWithQuotes(i);
30+
i++;
31+
}
32+
// dealWithLastWord();
33+
return 0;
34+
}
35+
36+
int dealWithSpace(int index)
37+
{
38+
char tempArray[10];
39+
int k = 0, i = 0;
40+
41+
flag1 = index + 1;
42+
index++;
43+
while(inputString[index] != (char)32 || inputString[index] != '\0')
44+
index++;
45+
for(i = flag1, k = 0; i <= index; i++, k++)
46+
tempArray[k] = inputString[i];
47+
48+
printf("Token: %s", tempArray);
49+
index += 1;
50+
return index;
51+
}
52+
53+
int dealWithQuotes(int index)
54+
{
55+
char tempArray[10];
56+
int i = 0, k = 0;
57+
58+
quoteFlag1 = index + 1;
59+
while(inputString[index] != (char)34 || inputString[index] != '\0')
60+
index++;
61+
for(i = flag1, k = 0; i <= index; i++, k++)
62+
tempArray[k] = inputString[i];
63+
printf("Token: %s", tempArray);
64+
index += 2;
65+
return index;
66+
67+
}
68+
69+
// void dealWithLastWord(){
70+
// int i;
71+
// printf("Token: ");
72+
// for(i = flag1; i < strlen(inputString); i++)
73+
// printf("%c", inputString[i]);
74+
// printf("\n");
75+
// }
76+

DisectingString/a.out

8.86 KB
Binary file not shown.

Login/.login2.c.swp

-12 KB
Binary file not shown.

Login/.vscode/.BROWSE.VC.DB-shm

0 Bytes
Binary file not shown.

Login/.vscode/.BROWSE.VC.DB-wal

-125 KB
Binary file not shown.

Login/a.out

-13.1 KB
Binary file not shown.

Login/login2.c

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ FILE *fp = fopen("vault.txt", "rb");
8787
while(fread(&search, sizeof(struct record), 1, fp) != 0){
8888
if( strcmp(tempUserName, search.userName) == 0 && strcmp(tempPassword, search.password) == 0 ){ printf("%s", spaces);
8989
printf("Acccess Granted!\n");
90+
system("./mainprogram");
9091
return;
9192
}
9293

Login/login2.c~

-104
This file was deleted.

Login/mainprogram

13.2 KB
Binary file not shown.

Login/notes.txt

Whitespace-only changes.

Login/vault.txt

40 Bytes
Binary file not shown.

SP9thprogram

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
#include <unistd.h>
2+
#include <signal.h>
3+
#include <sys/types.h>
4+
#include <stdlib.h>
5+
#include <sys/wait.h>
6+
#include <sys/ipc.h>
7+
#include <sys/shm.h>
8+
#include <iostream>
9+
using namespace std;
10+
11+
12+
void producerHandler (int sig);
13+
void consumerHandler (int sig);
14+
15+
16+
const int BUFFER_SIZE = 5;
17+
struct SharedMemory
18+
{
19+
int numItems;
20+
int in, out;
21+
int buffer[BUFFER_SIZE];
22+
};
23+
24+
int shmid;
25+
SharedMemory *shmptr;
26+
pid_t child_pid;
27+
28+
int main()
29+
{
30+
31+
shmid = shmget(IPC_PRIVATE, sizeof(struct SharedMemory), 0666|IPC_CREAT);
32+
if (shmid == -1) {
33+
cerr << "shmget failed\n";
34+
exit(EXIT_FAILURE);
35+
}
36+
37+
38+
child_pid = fork();
39+
if (child_pid < 0) {
40+
cerr << "Fork Failed\n";
41+
exit(-1);
42+
}
43+
44+
if (child_pid == 0)
45+
{
46+
signal (SIGINT, consumerHandler);
47+
cout << "I am the consumer\n"
48+
<< "Consumer will sleep for a bit to make sure that producer "
49+
<< "initializes shared memory" << endl;
50+
sleep(1);
51+
52+
53+
cout << "Consumer: attaching shared memory\n";
54+
shmptr = (SharedMemory*)shmat(shmid, NULL, 0);
55+
if (shmptr == (SharedMemory *)-1) {
56+
cerr << "shmat failed\n";
57+
exit(EXIT_FAILURE);
58+
}
59+
cout << "Consumer: memory attached\n";
60+
61+
62+
while (true)
63+
{
64+
cout << "Consumer attempting to read\n";
65+
66+
while (shmptr->numItems == 0)
67+
;
68+
69+
shmptr->numItems--;
70+
71+
int nextConsumed = shmptr->buffer[shmptr->out];
72+
73+
cout << "Consumer read: " << nextConsumed << " at index " << shmptr->out << endl;
74+
75+
shmptr->out = (shmptr->out + 1) % BUFFER_SIZE;
76+
77+
sleep(rand()%3); // 0-3 seconds
78+
79+
}
80+
81+
}
82+
else
83+
{
84+
signal (SIGINT, producerHandler);
85+
cout << "I am the producer\n";
86+
87+
88+
cout << "Producer: attaching shared memory\n";
89+
shmptr = (SharedMemory *)shmat(shmid, (void *)0,0);
90+
if (shmptr == (SharedMemory *)-1) {
91+
cerr << "shmat failed\n";
92+
exit(EXIT_FAILURE);
93+
}
94+
cout << "Producer: memory attached\n";
95+
96+
97+
cout << "Producer: initializing shared memory\n";
98+
shmptr->in = 0;
99+
shmptr->out = 0;
100+
shmptr->numItems = 0;
101+
cout << "Producer: shared memory initialized\n";
102+
103+
104+
while (true)
105+
{
106+
cout << "Producer attempting write\n";
107+
108+
int nextProduced = rand()%10000;
109+
110+
while (shmptr->numItems == BUFFER_SIZE)
111+
; // do nothing
112+
113+
shmptr->buffer[shmptr->in] = nextProduced;
114+
115+
cout << "Producer wrote: " << nextProduced << " at index " << shmptr->in << endl;
116+
117+
shmptr->in = (shmptr->in + 1) % BUFFER_SIZE;
118+
119+
shmptr->numItems++; // CRITICAL section
120+
121+
sleep(rand()%3); // 0-3 seconds
122+
123+
}
124+
}
125+
}
126+
127+
128+
void consumerHandler (int sig)
129+
{
130+
131+
cout << "Consumer detaching shared memory\n";
132+
if (shmdt(shmptr) == -1) {
133+
cerr << "shmdt failed\n";
134+
exit(EXIT_FAILURE);
135+
}
136+
exit(EXIT_SUCCESS);
137+
}
138+
139+
140+
void producerHandler (int sig)
141+
{
142+
143+
cout << "Producer killing consumer\n";
144+
kill (child_pid, SIGINT);
145+
wait (NULL);
146+
147+
cout << "Producer detaching and removing shared memory\n";
148+
149+
if (shmdt(shmptr) == -1) {
150+
cerr << "shmdt failed\n";
151+
exit(EXIT_FAILURE);
152+
}
153+
154+
if (shmctl(shmid, IPC_RMID, 0) == -1) {
155+
cerr << "shmctl(IPC_RMID) failed\n";
156+
exit(EXIT_FAILURE);
157+
}
158+
exit(EXIT_SUCCESS);
159+
}
160+

calendar/a.out

4.28 KB
Binary file not shown.

calendar/calendarOwn

Whitespace-only changes.

0 commit comments

Comments
 (0)