Lab 2 is in fact the first lab in the set of ones we actually do. It's not particularly hard, but consumed some time for me
57 lines
1.6 KiB
C
57 lines
1.6 KiB
C
/*
|
|
* "THE CHEESE-WARE LICENSE" (Revision 1):
|
|
* <ElectronixTM> wrote this file. As long as you retain this notice
|
|
* you can do whatever you want with this stuff. If we meet some day,
|
|
* and you think this stuff is worth it, you can buy me some cheese
|
|
* in return
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <sys/wait.h>
|
|
#include <assert.h>
|
|
|
|
#include "common.h"
|
|
|
|
#define PROG_PREFIX "father: "
|
|
#define log(fmt, args...) printf(PROG_PREFIX fmt "\n", ##args)
|
|
|
|
#define CMD_SIZE 256
|
|
|
|
int main(int argc, const char **argv)
|
|
{
|
|
if (argc != 2) {
|
|
log("Wrong program usage. 1 argument shuld be passed - program mode");
|
|
log("%d - wait for child", WAIT_FOR_CHILD);
|
|
log("%d - father doesn't wait for child", DONT_WAIT_FOR_CHILD);
|
|
log("%d - father doesn't wait for child, but child finishes",
|
|
ZOMBIE_PROCESS);
|
|
return EXIT_FAILURE;
|
|
}
|
|
long mode = strtol(argv[1], NULL, 10);
|
|
if (fork() == 0) {
|
|
execl("son", "son", argv[1], NULL);
|
|
}
|
|
char cmd[CMD_SIZE] = {0};
|
|
snprintf(cmd, CMD_SIZE, "ps fx > task_dump-%ld", mode);
|
|
if (mode == WAIT_FOR_CHILD) {
|
|
system(cmd);
|
|
int status = 0;
|
|
int ret = wait(&status);
|
|
if (ret < 0) {
|
|
log("waiting for son ended with error code %d", ret);
|
|
return EXIT_FAILURE;
|
|
}
|
|
} else if (mode == DONT_WAIT_FOR_CHILD) {
|
|
system(cmd);
|
|
} else if (mode == ZOMBIE_PROCESS) {
|
|
sleep(2);
|
|
system(cmd);
|
|
} else {
|
|
log("Invalid argument. No mode correspoiding to %ld", mode);
|
|
return EXIT_FAILURE;
|
|
}
|
|
return EXIT_SUCCESS;
|
|
}
|