Some Questions of Process API(1)

来源:互联网 发布:java什么是方法的定义 编辑:程序博客网 时间:2024/05/21 04:01

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 = 2hello, 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 = 3hello, 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 worldhello 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)hellowc = 1576goodby
0 0
原创粉丝点击