Home > Software engineering >  C program, creating processes , PID
C program, creating processes , PID

Time:10-12

The task is as follows

Write a program that would run another process in memory and leave it running in an infinite loop. When the program is restarted, it must remove the previously started process from memory (you can use kill).

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <spawn.h>
#include <sys/wait.h>

int main(void){
    int pid = getpid(); // we find out the PID of the current process and store it in a variable
    FILE *file = fopen("example.txt", "r"); // getting information from a file about a child process
    int filePid = 0;
    fscanf(file, "%d", filePid);
    fclose(file);
    
    switch (filePid){
        case -1:{ // if there is no child process, then run it and write the PID to a file
            filePid = fork();
            file = fopen("example.txt", "w");
            fprintf(file, "%d", filePid);
            fclose(file);
            break;
        }
        case 0:{ // if this process is a child, then we go into an infinite loop
            for(;;){
                sleep(7); // waiting for seven seconds so that the system is not heavily loaded
            }
            break;
        }
        default:{ // if this program is started again with a child process, then we send a signal to the child process
            kill(filePid, SIGKILL); // we send a signal to the child process so that it ends, and after that we write the information to the file
            file = fopen("example.txt", "w"); // we write information to the file that the child process is missing
            fprintf(file, "%d", -1);
            fclose(file);
        }
    }
    return EXIT_SUCCESS;
}

enter image description here /Yes, I have to do it through the qnx operating system./

the errors are as follows..I'm a little confused with getpid, because I haven't used the pid variable anywhere. enter image description here

and another mistake.

enter image description here

I will be grateful for your help.since I'm a little confused...

UPD:

I can't get the value 0

enter image description here

UPD: how could it execute both cases, i mean "if" and "else" blocks at the same time?

enter image description here

CodePudding user response:

how could it execute both cases, i mean "if" and "else" blocks at the same time?

You have to have a clear understanding of how fork works, when you use fork two identical processes are created parent and child and they run simultaneously, when you say filePid = fork() and if the operation is successful then the parent process will hold the process id of the child process and child process will hold 0. So here in parent process filePid == child process ID and in child process filePid == 0.

See man fork

On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in the parent, no child process is created, and errno is set appropriately.

So whatever you put in the if (filePid == 0) block will be executed by the child process and whatever you put in the else block will be executed by the parent process simultaneously.

This will be helpful if you want to know more about fork.

CodePudding user response:

fork() system call creates a child process and return its pid so once child is created you have two process running same instruction after fork() one is checking pid is greater which means its parent which gets child pid and in child context OS will set the pid value to 0 so it will enter condition block intended for child

  • Related