|
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> |
2 | 22 | #include<stdio.h>
|
3 | 23 | #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> |
33 | 25 |
|
34 |
| -} |
| 26 | +char msg[] = "Second task in CS392 final exam"; |
35 | 27 |
|
| 28 | +char revmsg[] = "maxe lanif 293SC ni ksat dnoceS"; |
36 | 29 |
|
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() { |
84 | 31 |
|
85 |
| -} |
| 32 | + //pipe to pass data from parent to child |
| 33 | + int p2c_pipe[2]; |
86 | 34 |
|
| 35 | + //pipe to pass data from child to parent |
| 36 | + int c2p_pipe[2]; |
87 | 37 |
|
88 |
| -int main(int argc, char ** argv){ |
| 38 | + char buffer[50]; |
89 | 39 |
|
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 | + } |
91 | 44 |
|
92 |
| - printf(" Test case 1:\n"); |
| 45 | + int pid = fork(); |
| 46 | + if (pid == -1) { |
| 47 | + perror("Cannot fork child\n"); |
| 48 | + exit(1); |
| 49 | + } |
93 | 50 |
|
94 |
| - char *strlist[5] = {"String1", "STring1", "String1extended", "String", "StRING1"}; |
| 51 | + if (pid == 0) { |
| 52 | + /*This is the child process*/ |
95 | 53 |
|
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 */ |
97 | 91 |
|
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 |
100 | 99 |
|
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 | + } |
105 | 105 |
|
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 |
109 | 107 |
|
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); |
112 | 110 |
|
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 ========== */ |
117 | 119 |
|
118 |
| - return 0; |
119 |
| -} |
120 | 120 |
|
| 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); |
121 | 125 |
|
| 126 | + |
| 127 | + } |
| 128 | + return 0; |
| 129 | +} |
0 commit comments