Compare commits
No commits in common. "bc7045ddccf3762d749b97f8d8d41c1a7d76826e" and "a2a6040d5457274c70bf7b12fb3a5a1d3a932ced" have entirely different histories.
bc7045ddcc
...
a2a6040d54
3
.gitignore
vendored
3
.gitignore
vendored
@ -3,6 +3,3 @@ cmake-build-debug
|
|||||||
CMakeLists.txt
|
CMakeLists.txt
|
||||||
.idea
|
.idea
|
||||||
*.o
|
*.o
|
||||||
|
|
||||||
# Apple
|
|
||||||
.DS_Store
|
|
@ -1,5 +1,5 @@
|
|||||||
//
|
//
|
||||||
// Written for Computer Networks and Systems lab classes
|
// Wrote for Computer Networks and Systems lab classes
|
||||||
// AUTHOR : Sergiusz Warga
|
// AUTHOR : Sergiusz Warga
|
||||||
|
|
||||||
void child_task() {
|
void child_task() {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
//
|
//
|
||||||
// Written for Computer Networks and Systems lab classes
|
// Wrote for Computer Networks and Systems lab classes
|
||||||
// AUTHOR : Sergiusz Warga
|
// AUTHOR : Sergiusz Warga
|
||||||
|
|
||||||
#ifndef AAE_CNAS_LABS_CHILD_FUNCTIONS_H
|
#ifndef AAE_CNAS_LABS_CHILD_FUNCTIONS_H
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
//
|
//
|
||||||
// Written for Computer Networks and Systems lab classes
|
// Wrote for Computer Networks and Systems lab classes
|
||||||
// AUTHOR : Sergiusz Warga
|
// AUTHOR : Sergiusz Warga
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
//
|
//
|
||||||
// Written for Computer Networks and Systems lab classes
|
// Wrote for Computer Networks and Systems lab classes
|
||||||
// AUTHOR : Sergiusz Warga
|
// AUTHOR : Sergiusz Warga
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
//
|
//
|
||||||
// Written for Computer Networks and Systems lab classes
|
// Wrote for Computer Networks and Systems lab classes
|
||||||
// AUTHOR : Sergiusz Warga
|
// AUTHOR : Sergiusz Warga
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
@ -1,82 +1,57 @@
|
|||||||
//
|
//
|
||||||
// Written for Computer Networks and Systems lab classes
|
// Wrote for Computer Networks and Systems lab classes
|
||||||
// AUTHOR : Sergiusz Warga
|
// AUTHOR : Sergiusz Warga
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/time.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
const char * get_timestamp() {
|
|
||||||
struct timeval now;
|
|
||||||
gettimeofday(&now, NULL);
|
|
||||||
char buff[127];
|
|
||||||
sprintf(&buff, "us : %d", now.tv_usec);
|
|
||||||
return buff;
|
|
||||||
}
|
|
||||||
|
|
||||||
void parent_before_child() {
|
void parent_before_child() {
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
printf("\nparent_before_child()\n");
|
printf("parent_before_child()\n");
|
||||||
pid = fork();
|
pid = fork();
|
||||||
if (pid == -1) { // If something went wrong.
|
if (pid == -1) { // If something went wrong.
|
||||||
perror("Fork: ");
|
perror("Fork: ");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
if (pid != 0) { // If this is a parent process.
|
if (pid != 0) { // If this is a parent process.
|
||||||
printf("===\n%s\nI am a parent.\nMy PID is: %d\n===", get_timestamp(), getpid());
|
printf("I am a parent.\n");
|
||||||
sleep(1);
|
kill(getpid(), SIGKILL);
|
||||||
kill(getpid(), SIGTERM);
|
|
||||||
} else {
|
} else {
|
||||||
printf("===\n%s\nI am a child.\nMy PID is: %d\nMy PPID is: %d\n===", get_timestamp(), getpid(), getppid());
|
|
||||||
sleep(1);
|
sleep(1);
|
||||||
printf("===\n%s\nI am a child.\nMy PID is: %d\nMy PPID is: %d\n===", get_timestamp(), getpid(), getppid());
|
printf("I am a child\n"); // This line will not be printed, as (usually) parent process will finish
|
||||||
sleep(1);
|
// its execution in less than 1 second.
|
||||||
printf("===\n%s\nI am a child.\nMy PID is: %d\nMy PPID is: %d\n===", get_timestamp(), getpid(), getppid());
|
|
||||||
// When parent is killed PPID of its child process becomes 1 (and so the init process becomes a new parent).
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void child_before_parent() {
|
void child_before_parent() {
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
printf("\nchild_before_parent()\n");
|
printf("child_before_parent()\n");
|
||||||
printf("PID before fork: %d\n", getpid());
|
|
||||||
pid = fork();
|
pid = fork();
|
||||||
if (pid == -1) { // If something went wrong.
|
if (pid == -1) { // If something went wrong.
|
||||||
perror("Fork: ");
|
perror("Fork: ");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
if (pid == 0) { // If this is a parent process.
|
if (pid == 0) { // If this is a parent process.
|
||||||
printf("===\n%s\nI am a child.\nMy PID is: %d\nMy PPID is: %d\n===", get_timestamp(), getpid(), getppid());
|
printf("I am a child.\n");
|
||||||
sleep(1);
|
kill(getpid(), SIGKILL);
|
||||||
kill(getpid(), SIGTERM);
|
|
||||||
} else {
|
} else {
|
||||||
printf("===\n%s\nI am a parent.\nMy PID is: %d\n===", get_timestamp(), getpid());
|
|
||||||
sleep(1);
|
sleep(1);
|
||||||
printf("===\n%s\nI am a parent.\nMy PID is: %d\n===", get_timestamp(), getpid());
|
printf("I am a parent.\n");
|
||||||
sleep(1);
|
|
||||||
printf("===\n%s\nI am a parent.\nMy PID is: %d\n===", get_timestamp(), getpid());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int status;
|
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
printf("%s: Grandparent process has PID: %d\n", get_timestamp(), getpid());
|
|
||||||
pid = fork();
|
pid = fork();
|
||||||
if (pid == 0) { // If child process
|
if (pid == 0) {
|
||||||
printf("%s: Process with PID %d will execute funs.\n", get_timestamp(), getpid());
|
|
||||||
child_before_parent();
|
child_before_parent();
|
||||||
sleep(5);
|
sleep(1);
|
||||||
parent_before_child();
|
parent_before_child();
|
||||||
sleep(5);
|
sleep(1);
|
||||||
} else { // If parent process
|
|
||||||
printf("%s: Grandparent's child process has PID: %d\n", get_timestamp(), pid);
|
|
||||||
sleep(12);
|
|
||||||
}
|
}
|
||||||
|
sleep(5);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
@ -1,36 +0,0 @@
|
|||||||
//
|
|
||||||
// Written for Computer Networks and Systems lab classes
|
|
||||||
// AUTHOR : Sergiusz Warga
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
void child_task() {
|
|
||||||
for (int i = 1; i <= 10; ++i) {
|
|
||||||
printf("%d\n", i);
|
|
||||||
fflush(stdout);
|
|
||||||
if (i < 10) sleep(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
pid_t pid;
|
|
||||||
|
|
||||||
pid = fork();
|
|
||||||
|
|
||||||
if (pid == -1) { // If something went wrong.
|
|
||||||
perror("Fork: ");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pid == 0) { // If this is a child process.
|
|
||||||
child_task();
|
|
||||||
exit(EXIT_SUCCESS);
|
|
||||||
}
|
|
||||||
|
|
||||||
wait();
|
|
||||||
printf("END OF WORK\n");
|
|
||||||
return 0;
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user