第8章习题

Exercises

8.1 In Figure 8.3, we said that replacing the call to _exit with a call to exit might cause the standard output to be closed and printf to return −1. Modify the program to check whether your implementation behaves this way. If it does not, how can you simulate this behavior?

int     globvar = 6;

int
main(void)
{
    int     var; /* automatic variable on the stack  */
    pid_t   pid;
    int i;
    char *buf = (char *)malloc(sizeof(char)*10);

    var = 88;
    printf("before vfork\n");   /* we don't flush stdio */
    if ((pid = vfork()) < 0) {
        err_say("vfork error");
    } else if (pid == 0) {      /* child */
        globvar++;              
        var++;
        fclose(stdout);
        exit(0);                /* child terminates */
    }

    /* parent continues here */
    i = printf("pid = %ld, glob = %d, var = %d\n", (long)getpid(), globvar, var);
    sprintf(buf, "%d\n",i);
    write(STDOUT_FILENO, buf, strlen(buf));
    exit(0);
}

8.2 Recall the typical arrangement of memory in Figure 7.6. Because the stack frames corresponding to each function call are usually stored in the stack, and because after a vfork the child runs in the address space of the parent, what happens if the call to vfork is from a function other than main and the child does a return from this function after the vfork? Write a test program to verify this, and draw a picture of what’s happening.

8.3 Rewrite the program in Figure 8.6 to use waitid instead of wait. Instead of calling
pr_exit, determine the equivalent information from the siginfo structure.
8.4 When we execute the program in Figure 8.13 one time, as in
./a.outtheoutputiscorrect.Butifweexecutetheprogrammultipletimes,onerightaftertheother,asin ./a.out ; ./a.out ; ./a.out
output from parent
ooutput from parent
ouotuptut from child
put from parent
output from child
utput from child
the output is not correct. What’s happening? How can we correct this? Can this problem
happen if we let the child write its output first?
8.5 In the program shown in Figure 8.20, we call execl, specifying the pathname of the
interpreter file. If we called execlp instead, specifying a filename of testinterp, and if
the directory /home/sar/bin was a path prefix, what would be printed as argv[2] when
the program is run?
8.6 Write a program that creates a zombie, and then call system to execute the ps(1) command
to verify that the process is a zombie.
8.7 We mentioned in Section 8.10 that POSIX.1 requires open directory streams to be closed
across an exec. Verify this as follows: call opendir for the root directory, peek at your
system’s implementation of the DIR structure, and print the close-on-exec flag. Then open
the same directory for reading, and print the close-on-exec flag

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值