forked from SoftwareSystemsLaboratory/systems-code-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
39 lines (32 loc) · 894 Bytes
/
main.c
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
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
int main(int argc, char* argv[])
{
int SomeValue = 100;
int pid = fork();
int fd = open("test_file", O_WRONLY|O_CREAT|O_TRUNC, 0666);
const char *parentMessage = "1111111";
const char *childMessage = "22222222222222\n";
if(pid > 0)
{
printf("hello from the parent process, chid pid = %d\n", pid);
sleep(2);
printf("parent's SomeValue = %d\n", SomeValue);
write(fd, parentMessage, strlen(parentMessage) * sizeof(char));
}
else if(pid == 0)
{
printf("hello from the child process\n");
SomeValue = 200;
printf("child's SomeValue = %d\n", SomeValue);
write(fd, childMessage, strlen(childMessage) * sizeof(char));
}
else
{
printf("fork() failed!!\n");
}
close(fd);
return 0;
}