[운영체제] 프로세스 생성 실습 및 퀴즈

Woohyun Shin·2022년 2월 27일
0

운영체제

목록 보기
3/3
#include <stdio.h>
#include <unistd.h>
#include <wait.h>

int value=5;

int main()
{
    pid_t pid;

    pid = fork();

    printf("Hello, Process! %d\n",pid);

    return 0;
}

#include <stdio.h>
#include <unistd.h>
#include <wait.h>

int value=5;

int main()
{
    pid_t pid;

    pid = fork();

    if(pid>0) wait(NULL); //부모 프로세스를 wait 상태로 변경
    printf("Hello, Process! %d\n",pid);

    return 0;
}

#include <stdio.h>
#include <unistd.h>
#include <wait.h>

int value=5;

int main()
{
    pid_t pid;

    pid = fork();

    if(pid==0){ //child process
        value+=15;
        return 0;
    }
    else if(pid>0){ //parent process
        wait(NULL);
        printf("Parent: value = %d\n",value);
    }
    
}

#include <stdio.h>
#include <unistd.h>

//How many processes are created?

int main(){

	fork();
    fork();
    fork();
    
}

답은 8이다. 먼저 첫 번째 fork()에서 두 개의 프로세스가 생성되고, 그 두 개의 프로세스에서 fork()를 통해 네 개, 마지막 fork()에서 총 8개가 된다.

#include <stdio.h>
#include <unistd.h>
#include <wait.h>

int value=5;

int main()
{
    pid_t pid;

    pid = fork();

    if(pid==0){ //child process
        execlp("/bin/ls","ls",NULL);
        printf("LINE J\n"); //실행되지 않음(ls로 덮어씌워졌기 때문)
    }
    else if(pid>0){ //parent process
        wait(NULL);
        printf("Child Complete\n");
    }

    return 0;

}

#include <stdio.h>
#include <unistd.h>
#include <wait.h>
#define SIZE 5

int nums[SIZE]={0,1,2,3,4};

int main()
{
    pid_t pid;
    int i;
    pid = fork();

    if(pid==0){
        for(i=0;i<SIZE;i++){
            nums[i]*=i;
            printf("CHILD: %d \n",nums[i]);
        }
    }
    else if(pid>0){
        wait(NULL);
        for(i=0;i<SIZE;i++){
            printf("PARENT: %d \n",nums[i]);
        }
    }

    return 0;

}

Quiz


1)


1) (X)
2) (O)


5)


2)


4)


1) (X)
4) (O)


4) (X)
2) (O)

profile
조급함보다는 꾸준하게

0개의 댓글