Skip to content

Commit

Permalink
Finished first two exercises of worksheet 2
Browse files Browse the repository at this point in the history
  • Loading branch information
RisingFisan committed Mar 16, 2020
1 parent 6c4e22d commit ab082db
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Guião 2/Ex1/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <stdio.h>
#include <unistd.h> /*chamadas ao sistema: defs e decls essenciais*/
#include <sys/wait.h> /*chamadas wait*() e macros relacionadas*/

int main(int argc, char const *argv[]) {
pid_t pid = getpid();
pid_t ppid = getppid();
printf("The PID is %d.\nThe PPID is %d.\n",(int)pid,(int)ppid);
return 0;
}
21 changes: 21 additions & 0 deletions Guião 2/Ex2/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(int argc, char const *argv[]) {
pid_t pid;
if((pid = fork()) == 0) {
pid_t childPID = getpid();
pid_t childPPID = getppid();
printf("Child PID = %d\nChild PPID = %d\n", childPID, childPPID);
}
else {
pid_t parentPID = getpid();
pid_t parentPPID = getppid();
pid_t parentChildPID = pid;
printf("Parent PID = %d\nParent PPID = %d\nChild PID from Parent = %d\n\n", parentPID, parentPPID, parentChildPID);
wait(NULL);
}
return 0;
}

0 comments on commit ab082db

Please sign in to comment.