Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial commit #384

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added ex1/a.out
Binary file not shown.
14 changes: 13 additions & 1 deletion ex1/ex1.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,19 @@

int main(void)
{
// Your code here

int x = 50;
int pid = fork();

if (pid == 0)
{
printf("Child value: %i\n", x);
}
else
{

printf("Parent value: %i\n", pid);
}

return 0;
}
Binary file added ex2/a.out
Binary file not shown.
22 changes: 18 additions & 4 deletions ex2/ex2.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Write a program that opens the text.txt file (with the `fopen()` library call) located in this directory
// and then calls `fork()` to create a new process. Can both the child and parent access the file descriptor
// Write a program that opens the text.txt file (with the `fopen()` library call) located in this directory
// and then calls `fork()` to create a new process. Can both the child and parent access the file descriptor
// returned by `fopen()`? What happens when they are written to the file concurrently?

#include <stdio.h>
Expand All @@ -8,7 +8,21 @@

int main(void)
{
// Your code here

FILE *fp;

fp = fopen("text.txt", "w+");

fprintf(fp, "%s %s", "Hello", "World");
fclose(fp);

int rc = fork();
if (rc == 0)
{
printf("I am the child, pid: %d\n", (int)getpid());
}
else
{
printf("I am the parent, pid: %d, pid of child: %d\n", (int)getpid(), rc);
}
return 0;
}
1 change: 1 addition & 0 deletions ex2/text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World
Binary file added ex3/a.out
Binary file not shown.
13 changes: 12 additions & 1 deletion ex3/ex3.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,18 @@

int main(void)
{
// Your code here
// printf("goodbye (pid: %d)\n", (int)getpid());
int rc = fork();

// child process starts executing here
if (rc == 0)
{
printf("Child: hello\n");
}
else
{
int wc = waitpid(rc, NULL, 0);
printf("Parent: goodbye\n");
}
return 0;
}
Binary file added ex4/a.out
Binary file not shown.
10 changes: 7 additions & 3 deletions ex4/ex4.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Write a program that calls `fork()` and then calls some form of `exec()`
// to run the program `/bin/ls`. Try a few variants of `exec()`, such as
// `execl()`, `execle()`, `execv()`, and others. Why do you think there
// to run the program `/bin/ls`. Try a few variants of `exec()`, such as
// `execl()`, `execle()`, `execv()`, and others. Why do you think there
// are so many variants of the same basic call?

#include <stdio.h>
Expand All @@ -10,7 +10,11 @@

int main(void)
{
// Your code here
printf("Hello World (pid: %d)\n", (int)getpid());

int rc = fork();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try and play around with exec calls that depend upon the forked process.


rc = execl("/bin/ls", "ls", NULL);

return 0;
}