Skip to content

Commit ce361c0

Browse files
authored
Add files via upload
1 parent 0ef7df1 commit ce361c0

File tree

4 files changed

+331
-221
lines changed

4 files changed

+331
-221
lines changed

Makefile

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
.PHONY: task1 task2 task3 clean
2+
3+
task1:
4+
@echo "Compiling task 1"
5+
@gcc -O0 -g -o task1 task1.c
6+
@echo "Running task 1"
7+
@./task1
8+
9+
task2:
10+
@echo "Compiling task 2"
11+
@gcc -O0 -g -o task2 task2.c
12+
@echo "Running task 2"
13+
@./task2
14+
15+
task3:
16+
@echo "Compiling task 3"
17+
gcc -O0 -g -o task3 task3.c -lpthread
18+
@echo "Running task 3"
19+
./task3
20+
21+
clean:
22+
rm task1 task2 task3 *.o

task1.c

+80-48
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,97 @@
1-
/* I pledge my honor that I have abided by the Stevens Honor System - Aparajita Rana*/
2-
#include<stdio.h>
3-
#include<string.h>
4-
5-
void cs392_reversestr(char * str){
6-
7-
/*Please write down your code here*/
8-
9-
//get the end value for our loop using length - thats our max iteration
10-
int length = strlen(str);
11-
12-
//temp values for store different individual characters these are
13-
//where we store and swap with
14-
int temp;
15-
int temp2;
16-
int temp3;
17-
18-
//for loop breakdown:
19-
//start at the first character, temp3 is the last, check if first is less than last
20-
//if so flip
21-
for (temp2 = 0, temp3 = length - 1; temp2 < temp3; temp2++, temp3--)
22-
{
23-
//essentially a swap method except we are flipping specific characters
24-
temp = str[temp2];
25-
str[temp2] = str[temp3];
26-
str[temp3] = temp;
27-
}
28-
}
291

2+
/*
3+
I pledge my honor that I have abided by the Stevens Honor System - Aparajita Rana
4+
* This program implements a signal-handling program:
5+
* Functionality Description:
306
31-
int main(int argc, char ** argv){
7+
This program will register a signal handler for sigint (SIGINT), using the sigaction interface (with the sa_sigaction field in the second argument to sigaction). When a sigint signal is received (by pressing ctrl + c), the handler will increase a "global variable" called "sigcount" by 1
8+
9+
After the program starts, it will run an infinite loop, where each iteration will print a message and then sleep for 1 second.
3210
33-
int index;
34-
char src[128];
35-
char dst[128];
11+
You can press ctrl + c to trigger the sigint signal. After you do it for 3 times, the the loop will be broken and the program will terminate.
3612
37-
printf("CS_392 midterm task 1: \n");
3813
39-
index = 1;
14+
* Testing points:
15+
You are required to find out 10 errors in the following piece of code. You need to find at least 8 errors to get full score.
4016
41-
while(index <= 5){
17+
18+
HINT:
4219
43-
printf(" Test case %d:\n", index);
20+
Note that all the errors are marked with hints
4421
45-
memset(src, 0, 128);
46-
memset(dst, 0, 128);
22+
Please find syntax and grammar errors that prevent the building
23+
Pay attention to compiler warnings (if all syntax and grammar errors are fixed, you will not see any compiler warnings)
4724
48-
strcpy(src, argv[index]);
49-
strcpy(dst, argv[index+5]);
25+
Please find the errors that mismatch the above functionality descriptions
5026
51-
cs392_reversestr(src);
52-
53-
printf(" Original string: \"%s\"; Reversed string: \"%s\"; \n", argv[index], src);
27+
Please find the missing of error checking
28+
29+
Please find other errors based on the hints (you may need to change the locations of some code)
30+
31+
32+
To test this case, run "make -s task1"
33+
34+
After fixing all the errors, the program will run an infinite loop and after you press ctrl + c for 3 times, the program will terminate.
35+
36+
In case you do not find all the errors and you cannot kill your program by ctrl + c, try ctrl + d and then run "pkill task1"
5437
55-
if(strcmp(src, dst) == 0 )
56-
printf(" === Result: PASSED === \n\n");
57-
else
58-
printf(" === Result: FAILED === \n\n");
38+
*
39+
*/
5940

60-
index += 1;
41+
// SIGNAL.H Hint 1: something missing?
42+
#include <stdio.h>
43+
#include <unistd.h>
44+
#include <stdlib.h>
45+
#include <string.h>
46+
#include <signal.h>
47+
int sigcount; //DONE - global Hint 3: is this variable at the correct location? Check the description to understand its scope
48+
49+
//DONE Hint 2: does this handler have a correct prototype? You can google the requirement of the sigaction interface
50+
static void hdl (int sig, siginfo_t *siginfo, void *context){
51+
sigcount++;
52+
printf ("Received Signal, PID: %ld, UID: %ld\n", (long)siginfo->si_pid, (long)siginfo->si_uid);
53+
54+
}
55+
56+
int main (int argc, char **argv){
57+
58+
struct sigaction act;
59+
60+
//DONE Hint 4: Missing any initialization?
61+
sigcount=0;
62+
memset(&act, '\0',sizeof(act));
63+
//Some code here?
64+
65+
66+
/* Use the sa_sigaction field */
67+
act.sa_sigaction=hdl;
68+
69+
/* Setup the flag to tell sigaction() to use the sa_sigaction field */
70+
//DONE Hint 5: is it setting the correct flag?
71+
act.sa_flags = SA_SIGINFO;
72+
73+
//DONE added & Hint 6+7: Are all arguments correctly passed to the following function?
74+
//There are two errors related to the first two arguments.
75+
//Please check both the above functionality description and the prototype of the "sigaction" interface
76+
int sigret = sigaction(SIGINT, &act, NULL);
77+
78+
//DONE Hint 8: missing something after a system level interface?
79+
if(sigret<0){
80+
perror("Error: sigaction");
81+
return 1;
6182
}
6283

84+
while(1){
85+
//DONE Hint 9: will this condition check work correctly?
86+
if(sigcount == 3){
87+
//DONE fix the %d Hint 10: any problem in the following format string? Think about types
88+
printf("You have hit ctrl+c for %d times. Program terminated\n", sigcount);
89+
exit(0);
90+
}
91+
printf("Hello, please presse ctrl+c\n");
92+
sleep(1);
93+
}
94+
6395
return 0;
6496
}
6597

task2.c

+109-101
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,129 @@
1-
/* I pledge my honor that I have abided by the Stevens Honor System - Aparajita Rana*/
1+
/**
2+
I pledge my honor that I have abided by the Stevens Honor System - Aparajita Rana
3+
In this task, you are required to implement a two-directional inter-process communication with two pipes.
4+
5+
In the "main" function, we have defined two pipes --- "p2c_pipe" for sending data from the parent to child and "c2p_piep" for sending data from the child to the parent.
6+
7+
In the "main" function, we have prepared the parent and child process. You have to finish the code for both of them.
8+
9+
For the parent process, you need to send the string carried by the "msg" array to the child process through "p2c_pipe"; Then you need to read data back from the child process via "c2p_pipe". Noe that you have to save the data from the child to an array called "buffer" (which is defined in the "main" function).
10+
11+
12+
For the child process, you need to read data from the parent process through "p2c_pipe", REVERSE the string, and then send the REVERSED string back to the child via "c2p_pipe".
13+
14+
In both the parent and the child process, you will have to close the unneeded ends of the two pipes.
15+
16+
You can assume the data is a string and it always has less than 50 bytes.
17+
18+
**/
19+
20+
#include<unistd.h>
21+
#include<stdlib.h>
222
#include<stdio.h>
323
#include<string.h>
4-
/*int sort_helper(char* a){
5-
char * i;
6-
int ascii;
7-
int sum;
8-
for (i=a; *i; i++)
9-
{
10-
//sprintf(ascii, " %d",i);
11-
ascii = (int) i;
12-
sum+=ascii;
13-
}
14-
return sum;
15-
}*/
16-
17-
//my strcomp method -> takes in two val returns 0 if equal like basic strcmps
18-
int my_strcmp(char *val1, char *val2){
19-
int val=0;
20-
//if this specific character is equal to incremented val2
21-
while (*val1 == *val2++)
22-
{
23-
//if incremened val1 is zero
24-
if (*val1++ == 0)
25-
{
26-
return 0;
27-
}
28-
}
29-
//otherwise, the two string are not equal
30-
//return pointers ascii val
31-
val=*val1 - *--val2;
32-
return val;
24+
#include<sys/types.h>
3325

34-
}
26+
char msg[] = "Second task in CS392 final exam";
3527

28+
char revmsg[] = "maxe lanif 293SC ni ksat dnoceS";
3629

37-
void cs392_str_sort(char ** strlist, int number){
38-
//IGNORE - Old Code
39-
//strl=strlist;
40-
//int n=number;
41-
/* Please write down your code here
42-
int c; int d;
43-
char* swap;
44-
for (c = 0 ; c < n - 1; c++)
45-
{
46-
for (d = 0 ; d < n - c - 1; d++)
47-
{
48-
if(my_strcmp(strlist[c],strlist[d])==1)
49-
{
50-
swap = strlist[c];
51-
strlist[c] = strlist[d];
52-
strlist[d] = swap;
53-
}
54-
}
55-
}*/
56-
57-
int loc;
58-
//checker tells us when to excit
59-
int checker=1;
60-
//reduces list and keeps track of sorted vals
61-
int val=number-1;
62-
char* temp;
63-
64-
//bubble sort using while loop
65-
while(checker)
66-
{
67-
checker = 0;
68-
//iterate through all of the strings
69-
for (loc = 0; loc < val; loc++)
70-
{
71-
//utilize my compare method, this means strlist[loc+1] was bigger
72-
if (my_strcmp(strlist[loc], strlist[loc+1]) > 0)
73-
{
74-
//this is my swap -> could be another function but left here
75-
//switch str[loc] and str[loc+1]
76-
temp=strlist[loc];
77-
strlist[loc]=strlist[loc+1];
78-
strlist[loc+1]=temp;
79-
checker=1;
80-
}
81-
}
82-
val--;
83-
}
30+
int main() {
8431

85-
}
32+
//pipe to pass data from parent to child
33+
int p2c_pipe[2];
8634

35+
//pipe to pass data from child to parent
36+
int c2p_pipe[2];
8737

88-
int main(int argc, char ** argv){
38+
char buffer[50];
8939

90-
printf("CS_392 midterm task 2: \n");
40+
if(pipe(p2c_pipe) || pipe(c2p_pipe)) {
41+
perror("Cannot create pipes\n");
42+
exit(1);
43+
}
9144

92-
printf(" Test case 1:\n");
45+
int pid = fork();
46+
if (pid == -1) {
47+
perror("Cannot fork child\n");
48+
exit(1);
49+
}
9350

94-
char *strlist[5] = {"String1", "STring1", "String1extended", "String", "StRING1"};
51+
if (pid == 0) {
52+
/*This is the child process*/
9553

96-
cs392_str_sort(strlist, 5);
54+
/* ========== Your code part 1 ends here ==========
55+
56+
You need to read a string from the p2c_pipe, reverse the string you received, and then send the reversed string back through c2p_pipe;
57+
You can assume the data has less than 50 bytes;
58+
Do not forget to close the ends that you do not need;
59+
*/
60+
close(p2c_pipe[1]); //write
61+
int val=read(p2c_pipe[0], buffer, 50); //read val store in pipe
62+
63+
//ERROR CHECK!
64+
if(val<0){
65+
perror("reading pipe error");
66+
return -1;
67+
}
68+
//strrev(buffer);
69+
//for loop to reverse the string
70+
int loc=0;
71+
72+
//backwords through it
73+
for(int x=(strlen(msg))-1;x>=0;x--){
74+
buffer[loc]=msg[x];
75+
loc++;
76+
}
77+
78+
close(p2c_pipe[0]); //close all read pipes
79+
close(c2p_pipe[0]);
80+
int val2=write(c2p_pipe[1],buffer,50); //send the reversed string back through c2p_pipe
81+
82+
//ERROR CHECK!
83+
if(val2<0){
84+
perror("writing pipe error");
85+
return -1;
86+
}
87+
/* ========== Your code part 1 ends here ========== */
88+
89+
}else {
90+
/* This is the parent process */
9791

98-
printf(" Correct results are: STring1, StRING1, String, String1, String1extended\n");
99-
printf(" Your results are: %s, %s, %s, %s, %s\n", strlist[0], strlist[1], strlist[2], strlist[3], strlist[4]);
92+
93+
/* ========== Your code part 2 begins here ==========
94+
95+
You need to send a string indicated by the "msg" buffer (defined above) to the child process through the p2c_piep, and then read the string returned back through the c2p_pipe. Make sure you save the data to the array called "buffer", which is also defined above.
96+
*/
97+
close(p2c_pipe[0]); // close read pipe p2c
98+
int val=write(p2c_pipe[1],msg,50); //write to child process through p2c_piep
10099

101-
if(strcmp(strlist[0], "STring1") == 0 && strcmp(strlist[1], "StRING1") == 0 && strcmp(strlist[2], "String") == 0 && strcmp(strlist[3], "String1") == 0 && strcmp(strlist[4], "String1extended") == 0)
102-
printf(" === Result: PASSED === \n\n");
103-
else
104-
printf(" === Result: FAILED === \n\n");
100+
//ERROR CHECK!
101+
if(val<0){
102+
perror("writing pipe error");
103+
return -1;
104+
}
105105

106-
char *strlist1[5] = {"string2", "s@ing2", "stringA", "sTRING2", "@tring2"};
107-
108-
cs392_str_sort(strlist1, 5);
106+
close(c2p_pipe[1]); // close write pipe c2p
109107

110-
printf(" Correct results are: @tring2, s@ing2, sTRING2, string2, stringA\n");
111-
printf(" Your results are: %s, %s, %s, %s, %s\n", strlist1[0], strlist1[1], strlist1[2], strlist1[3], strlist1[4]);
108+
//read the string returned back through the c2p_pipe
109+
int val2=read(c2p_pipe[0],buffer,50);
112110

113-
if(strcmp(strlist1[0], "@tring2") == 0 && strcmp(strlist1[1], "s@ing2") == 0 && strcmp(strlist1[2], "sTRING2") == 0 && strcmp(strlist1[3], "string2") == 0 && strcmp(strlist1[4], "stringA") == 0)
114-
printf(" === Result: PASSED === \n\n");
115-
else
116-
printf(" === Result: FAILED === \n\n");
111+
//ERROR CHECK!
112+
if(val2<0){
113+
perror("reading pipe error");
114+
return -1;
115+
}
116+
117+
118+
/* ========== Your code part 2 ends here ========== */
117119

118-
return 0;
119-
}
120120

121+
if(strcmp(buffer, revmsg) == 0)
122+
printf("============ Congrats! You passed the second assignment\n");
123+
else
124+
printf("============ Sorry! You failed the second assignment; The expected message is \"%s\", but the message the parent received is \"%s\"\n", revmsg, buffer);
121125

126+
127+
}
128+
return 0;
129+
}

0 commit comments

Comments
 (0)