Some Questions of Process API(1)

Record some code of calling Process API to enhance the understanding of these API.

Some questions and related coding.

It’s even more fun than it sounds!

0. Aside(RTFM – READ THE MAN PAGES)

When referring to a particular system call or library call, just reading the maunal/man pages.

Spending some time reading man pages is a key step in the growth of a systems programmer.

Something like:

type man fork() in your shell

or

just google C fork and you will get what you want.

Good luck to you.

1. Write a program that calls fork(). Before calling fork(), have the main process access a variable (e.g., x) and set its value to some-thing (e.g., 100). What value is the variable in the child process? What happens to the variable when both the child and parent change the value of x?

code

#include <stdio.h> // printf; fprintf; stderr
#include <stdlib.h> // exit()
#include <unistd.h> // fork(); getpid(); close(); execvp()

int main (int argc, char *argv[]) {
    printf("hello world (pid:%d)\n", (int)getpid());
    int x = 0;
    int rc = fork();
    if (rc < 0) { // fork failed, exit.
        fprintf(stderr, "fork failed\n");
        exit(1);
    }
    else if (rc == 0) { // child: redirect standard output to a file
        printf("hello, I am child (pid:%d)\n", (int)getpid());
        x += 2;
        printf("x = %d\n", x);
    }
    else { // parent goes down this path (main)
        printf("hello, I am parent of %d (pid:%d)\n", rc, (int)getpid());
        x += 2;
        printf("x = %d\n", x);
    }

    return 0;
}

result

hello world (pid:761)
hello, I am parent of 764 (pid:761)
x = 2
hello, I am child (pid:764)
x = 2

2. Write a program that opens a file (with the open() system call) and then calls fork() to create a new process. Can both the child and parent access the file descriptor returned by open()? What happens when they are writing to the file concurrently, i.e., at the same time?

2.1 Can both the child and parent access the file descriptor returned by open()?

gcc -Wall open.c

code

#include <stdio.h> // printf; fprintf; stderr
#include <stdlib.h> // exit()
#include <unistd.h> // fork(); getpid(); close(); execvp()
#include <fcntl.h> // open()
#include <sys/stat.h> // S_IRWXU
#include <assert.h> // assert()

int main (int argc, char *argv[]) {
    printf("hello world (pid:%d)\n", (int)getpid());
    int fd = open("./openFile", O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU); // open a file
    assert(fd > -1); // open failed, exit.

    int rc = fork();
    if (rc < 0) { // fork failed, exit.
        fprintf(stderr, "fork failed\n");
        exit(1);
    }
    else if (rc == 0) { // child: redirect standard output to a file
        printf("hello, I am child (pid:%d)\n", (int)getpid());
        printf("fd = %d\n", fd);
    }
    else { // parent goes down this path (main)
        printf("hello, I am parent of %d (pid:%d)\n", rc, (int)getpid());
        printf("fd = %d\n", fd);
    }

    return 0;
}

result

hello world (pid:1116)
hello, I am parent of 1117 (pid:1116)
fd = 3
hello, I am child (pid:1117)
fd = 3

2.2 What happens when they are writing to the file concurrently, i.e., at the same time?

I don’t know how to achieve at the same time, maybe as follows:

code

#include <stdio.h> // printf; fprintf; stderr
#include <stdlib.h> // exit()
#include <unistd.h> // fork(); getpid(); close(); execvp()
#include <fcntl.h> // open()
#include <sys/stat.h> // S_IRWXU
#include <assert.h> // assert()

int main (int argc, char *argv[]) {
    printf("hello world (pid:%d)\n", (int)getpid());
    int fd = open("./openFile", O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU); // open a file
    assert(fd > -1); // open failed, exit.

    int rc = fork();
    if (rc < 0) { // fork failed, exit.
        fprintf(stderr, "fork failed\n");
        exit(1);
    }
    else if (rc == 0) { // child: redirect standard output to a file
        printf("hello, I am child (pid:%d)\n", (int)getpid());
        printf("fd = %d\n", fd);
        ssize_t wc = write(fd, "hello world\n", 13);
        printf("wc = %zd\n", wc);
        assert(wc == 13);
        close(fd);
    }
    else { // parent goes down this path (main)
        printf("hello, I am parent of %d (pid:%d)\n", rc, (int)getpid());
        printf("fd = %d\n", fd);
        ssize_t wc = write(fd, "hello world\n", 13);
        printf("wc = %zd\n", wc);
        assert(wc == 13);
        close(fd);
    }

    return 0;
}

result

$ cat openFile

hello world
hello world

Mac OS X C open

这里写图片描述

这里写图片描述

3. Write another program using fork(). The child process should print “hello”; the parent process should print “goodbye”. You should try to ensure that the child process always prints first; can you do this without calling wait() in the parent?

code

#include <stdio.h> // printf; fprintf; stderr
#include <stdlib.h> // exit()
#include <unistd.h> // fork(); getpid(); close(); execvp()
#include <assert.h> // assert()

int main (int argc, char *argv[]) {
    printf("hello world (pid:%d)\n", (int)getpid());

    int rc = fork();
    if (rc < 0) { // fork failed, exit.
        fprintf(stderr, "fork failed\n");
        exit(1);
    }
    else if (rc == 0) { // child: redirect standard output to a file
        printf("hello, I am child (pid:%d)\n", (int)getpid());
        printf("hello\n");
    }
    else { // parent goes down this path (main)
        printf("hello, I am parent of %d (pid:%d)\n", rc, (int)getpid());
        int wc = wait(NULL);
        printf("wc = %d\n", wc);
        printf("goodby\n");
    }

    return 0;
}

result

hello world (pid:1575)
hello, I am parent of 1576 (pid:1575)
hello, I am child (pid:1576)
hello
wc = 1576
goodby
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值