-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhomework1_code.c
More file actions
213 lines (184 loc) · 5.26 KB
/
homework1_code.c
File metadata and controls
213 lines (184 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>
#include<fcntl.h>
#include<string.h>
#include<dirent.h>
// int main(int argc, char* argv){
// int val = 0;
// printf("before fork, val is: %d,\n", val);
// int fr = fork();
// if(fr == 0){
// val = 100;
// printf("this is the child (pid: %d), and value of val is: %d\n", (int) getpid(), val);
// }else if(fr > 0){
// val = 1000;
// int w = wait(NULL);
// printf("I am the parent (pid: %d), and the value is: %d\n", (int) getpid(), val);
// }else{
// fprintf(stderr, "fork failed \n");
// exit(1);
// }
// return 0;
// }
// Q2
// int main(){
// char* msg = "let's see what will happen \n";
// char* child_msg = "Here is the child !\n";
// char* parent_msg = "I am the parent !!\n";
// int fd = open("file.txt", O_CREAT | O_WRONLY, 0644);
// ssize_t bytes = write(fd, msg, strlen(msg));
// int fr = fork();
// if(fr == 0){
// printf("Child \n");
// ssize_t bytes = write(fd, child_msg, strlen(child_msg));
// }
// else if(fr > 0){
// printf("parent \n");
// ssize_t bytes = write(fd, parent_msg, strlen(parent_msg));
// }else{
// fprintf(stderr, "fork failed \n");
// exit(1);
// }
// return 0;
// }
// Q3
// first way:
// int main(int argc, char* argv[])
// {
// printf("Before fork !! \n");
// int fo = fork();
// if(fo == 0){
// char* arr[3];
// arr[0] = "echo";
// arr[1] = "HELLO";
// arr[2] = NULL;
// execvp(arr[0], arr);
// }else if(fo > 0){
// sleep(1);
// printf("Guten nacht \n");
// }else{
// printf("fork failed");
// }
// return 0;
// }
// second way:
// int main()
// {
// printf("Before fork !! \n");
// int fo = fork();
// if(fo == 0){
// printf("HELLO \n");
// fflush(stdout); // will print immediately.
// exit(0);
// }else if(fo > 0){
// sleep(1);
// printf("Good Bye \n");
// }else{
// printf("fork failed");
// }
// return 0;
// }
// Q4
// int main(){
// printf("hi \n");
// int fo = fork();
// if(fo < 0){
// perror("ERROR !! \n");
// exit(EXIT_FAILURE);
// }
// else if(fo == 0){
// char* commands[4];
// commands[0] = "ls";
// commands[1] = "/mnt/c/users/abdel/documents/books";
// commands[2] = NULL;
// execvp(commands[0], commands);
// }else{
// int w = wait(NULL);
// printf("Parent: %d \n", getpid());
// }
// return 0;
// }
// Q5
// int main(){
// printf("using wait () \n");
// int fo = fork();
// if(fo < 0){
// fprintf(stderr, "fork failed \n");
// exit(EXIT_FAILURE);
// }else if(fo == 0){
// int w = wait(NULL);
// printf("child with id: %d and wait is is : %d \n", getpid(), w);
// }else{
// printf("parent with id: %d \n ", getpid());
// }
// return 0;
// }
// Q6
// int main()
// {
// int fo = fork();
// if(fo == 0){
// printf("Success \n");
// exit(42);
// }else if(fo < 0){
// printf("Failure !! :( \n");
// exit(EXIT_FAILURE);
// }else{
// int status;
// pid_t wpid = waitpid(fo, &status, 0);
// if(WIFEXITED(status)) printf("Exited normally :) \n");
// else if(WIFSIGNALED(status)) printf("signal revceived \n");
// printf("parent \n");
// }
// return 0;
// }
//##### wiatpid() is useful when we to control a specific process using its id.#######
// Q7
// int main(){
// pid_t fo = fork();
// if(fo == 0){
// printf("Child \n");
// if(close(STDOUT_FILENO)==0){
// printf("Stdout is closed successfully \n");
// }
// printf("let us test this \n");
// }else if(fo < 0){
// printf("Fail \n");
// exit(EXIT_FAILURE);
// }else
// printf("Parent \n");
// return 0;
// }
//after closing stdout_fileno uding close() , anything we try to print will not be printed.
// Q8
int main(){
int fd[2];// fd[0]: read end fd[1]: write end.
pipe(fd);
pid_t fo1 = fork();
if(fo1 == 0){
close(fd[0]);
dup2(fd[1], STDOUT_FILENO); //send all printf() output to the pipe.
close(fd[1]);
printf("Hello from child1 \n");
exit(EXIT_SUCCESS);
}
pid_t fo2 = fork();
if(fo2 == 0){
close(fd[1]);
dup2(fd[0], STDIN_FILENO); //makes stdin read from the pipe instead of keyboard.
close(fd[0]);
// these previous lines show how linux shells are implemented.
char buf[100];
fgets(buf, sizeof(buf), stdin);// structure: pointer to char arr, max # of char includes \0, file obj represent the input stream.
printf("child 2 recieved: %s", buf);
exit(EXIT_SUCCESS);
}
close(fd[0]);
close(fd[1]);
wait(NULL);
wait(NULL);
return 0;
}