so i have this c program that creates child processes: int i, j, k; int ret = getpid();…
- User
so i have this c program that creates child processes:
int i, j, k; int ret = getpid(); i = fork();
j = fork();
k = fork();
printf("ret: %d, i: %d, j: %d, k: %d\n", ret, i, j, k);
the output is the follwoing:
ret: 8647, i: 8648, j: 8649, k: 8651 ret: 8647, i: 8648, j: 0, k: 8652 ret: 8647, i: 8648, j: 0, k: 0 ret: 8647, i: 0, j: 8650, k: 8653 ret: 8647, i: 0, j: 8650, k: 0 ret: 8647, i: 8648, j: 8649, k: 0 ret: 8647, i: 0, j: 0, k: 8654 ret: 8647, i: 0, j: 0, k: 0
Can you explain me how the assignment of the pids of each child is done and how it works and why it printed 8 times shouldn't be 2 prints per child and father?
- Assistant
- User
void main() { char buffer[128]; ... sprintf(buffer, " My PID is %d\n " , getpid()); write(1,buffer,strlen(buffer)); for (i = 0; i < 3; i++) { pid = fork(); if (pid == 0) doTask(); } while(1); // This is not good; what do we need? }
- Assistant
- User
explain this system call and its parameters: int execvp(const char file, const char argv[]);
- Assistant
The system call is part of a family of system calls in Unix-like operating systems that replace the current process image with a new process image. This is commonly used for running a new program from within a program. Unlike creating a new process (like with ), and other functions transform the calling process into a new process, running the program specified. The old process ceases to exist, and the newly loaded program starts executing from its function.
- User
what is he difference in putting _exit(0) and _exit(1)?
- Assistant
The function terminates the calling process immediately, without flushing stdio buffers or running any or functions that may have been registered with the current process. It's a system call provided by Unix-like operating systems for ending a process.
- User
what about _exit(4)?
- Assistant
- User
difference between concurrent and sequential programming schemas
- Assistant
- User
which one is concurrent and which one is sequential given this codes:
code A:
for (i = 0; i < num_cmds; i++) { pid = fork(); if (pid == 0) { exec(cmds[i]); _exit(0); } pid = wait(&exit_code); }
code B:
for (i = 0; i < num_cmds; i++) { pid = fork(); if (pid == 0) { exec(cmds[i]); _exit(0); } } while (wait(&exit_code) > 0);
- Assistant
Between the two code snippets provided, Code A executes commands sequentially, while Code B initiates a form of concurrent execution of the commands. Let's delve into each code block to understand why:
- User
but can a CPU do parallelization or is it all running concurrently?
- Assistant
- User
but if i have a single core cpu and i am trying to implement a c program that uses parallelization functions would that code run properly?
- Assistant