int main() { if(-1 == tell_wait()) { printf("fail to tell_wait\n"); return 0; } pid_t pid = fork(); if(0 > pid) { printf("fail to fork, errno: %d, errmsg: %s\n", errno, strerror(errno)); return 0; } else if(0 == pid) { child_process(); _exit(0); } parent_process(); int status; if(-1 == wait(&status)) { printf("[P]fail to wait for child process, errno: %d, errmsg: %s\n", errno, strerror(errno)); } return 0; }
int main(int argc, char* argv[]) { if (argc != 2) { fprintf(stderr, "Usage: %s <filename>\n", argv[1]); exit(1); } int fd; fd = open(argv[1], O_RDWR | O_CREAT | O_TRUNC, FILE_MODE); write(fd, "abcdef", 6); struct stat statbuf; fstat(fd, &statbuf); fchmod(fd, (statbuf.st_mode & ~S_IXGRP) | S_ISGID); tell_wait(); pid_t pid; pid = fork(); if (pid > 0) { write_lock(fd, 0, SEEK_SET, 0); tell_child(pid); waitpid(pid, NULL, 0); } else { wait_parent(); set_fl(fd, O_NONBLOCK); if (read_lock(fd, 0, SEEK_SET, 0) != -1) err_sys("child: read_lock successed"); printf("read_lock of already-locked region returns %d\n", errno); lseek(fd, 0, SEEK_SET); char buf[5]; if (read(fd, buf, 2) < 0) err_ret("read failed (mandatory locking works)"); else printf("read OK (no mandatory locking), buf = %2.2s\n", buf); } }
int main(int argc, char const *argv[]) { char buf[MAXSIZE]={'\0'}; char buf_r[MAXSIZE]={'\0'}; int fd[2]; pipe(fd); signal(SIGPIPE,fun); pid_t pid; int len = 0; int nr = 0; int sum = 0; int nread,len_r; //signal tongbu tell_wait(); //sgnal tongbu pid = fork(); if (pid <0) { perror("error"); /* code */ }else if (pid == 0) { /* code */ wait_father(); printf("the child process.....%d\n",getpid()); printf("the child process's father process id:%d\n",getppid()); close(fd[1]); while((nread = read(fd[0],buf,sizeof(buf)))!=-1){ tell_father(pid); setbuf(stdout,NULL);//set the stdout no buffer printf("the child write to stdout:"); write(STDOUT_FILENO,buf,nread); //the write to stdout is bo bug ,but the printf is bug len_r = strlen(buf); printf("len_r :%d\n",len_r ); for (int i = 0; (buf[i]!='\n'); i++) { sum +=i; printf("sum:%d\n",sum ); printf("buf[%d]:%c\t",i,buf[i] ); if (buf[i]=='\0') { break; } } printf("\n"); // printf("child read:%s\n",buf ); //the sentence is bug ,it is of buffer and \n } exit(0); }else{ // sleep(3); tell_child(pid); printf("the father process.......%d\n",getpid()); printf("the father process's father process id:%d\n",getppid()); close(fd[0]);//the sentense to invlole the SIGPIPE take place char * p = NULL; /* code */ while((p = fgets(buf,sizeof(buf),stdin))!=NULL){ len = strlen(buf); printf("the length :%d\n",len ); if (p[len] =='\n') { printf("%c\n",p[len -1] ); len--; p[len ] = '\0'; /* code */ } if (p[len -1] == '\0') { printf("fgets read the null in buffer\n"); /* code */ } fflush(stdin); int n = write(fd[1],buf,len); if (n==-1) { if (errno = EINTR) { printf("the failed is SIGINT\n"); /* code */ } printf("err write\n"); }else printf("father write to pipe successfully\n"); wait_child(); } int status; // the waitpid 's return val is the son process pid,if it wait to catch the son state successfully int ret = waitpid(pid,&status,0); if (ret == -1) { printf("wait child failed\n"); /* code */ }else printf("the wait id:%d\n", ret);//the ret == son pid } return 0; }
int csopen(char *name, int oflag) { pid_t pid; int len; char buf[10]; struct iovec iov[3]; static int fd[] = {-1, -1}; struct sockaddr_un addr; socklen_t addr_len; int new_fd = -1; //init server address addr.sun_family = AF_UNIX; strcpy(addr.sun_path, SERVER); addr_len = offsetof(struct sockaddr_un, sun_path)+strlen(addr.sun_path); if (0 > new_fd) { tell_wait(); if (0 > (pid = fork())) { perror("fork"); return EXIT_FAILURE; } else if (0 == pid) { //open socket and connect to server if (-1 == (fd[1] = socket(AF_UNIX, SOCK_STREAM, 0))) { perror("socket child"); return EXIT_FAILURE; } wait_parent(); if (-1 == connect(fd[1], (struct sockaddr*)&addr, addr_len)) { perror("connect"); return EXIT_FAILURE; } if (fd[1] != STDIN_FILENO && dup2(fd[1], STDIN_FILENO) != STDIN_FILENO) { perror("dup2 on stdin"); return EXIT_FAILURE; } if (fd[1] != STDOUT_FILENO && dup2(fd[1], STDOUT_FILENO) != STDOUT_FILENO) { perror("dup2 on stdin"); return EXIT_FAILURE; } if (0 > execl("bin/opend1", "opend1", (char*)0)) { perror("execl"); return EXIT_FAILURE; } return EXIT_SUCCESS; } //create a server and accept connections on it if (-1 == (fd[0] = socket(AF_UNIX, SOCK_STREAM, 0))) { perror("socket parent"); return EXIT_FAILURE; } if (-1 == unlink(SERVER)) { perror("unlink"); return EXIT_FAILURE; } if (-1 == bind(fd[0], (struct sockaddr*)&addr, addr_len)) { perror("bind"); return EXIT_FAILURE; } if (-1 == listen(fd[0], 3)) { perror("listen"); return EXIT_FAILURE; } tell_child(); if (-1 == (new_fd = accept(fd[0], (struct sockaddr*)&addr, &addr_len))) { perror("accept"); return EXIT_FAILURE; } } sprintf(buf, " %d", oflag); iov[0].iov_base = CL_OPEN " ";//string concatenation iov[0].iov_len = strlen(CL_OPEN)+1; iov[1].iov_base = name; iov[1].iov_len = strlen(name); iov[2].iov_base = buf; iov[2].iov_len = strlen(buf)+1; len = iov[0].iov_len+iov[1].iov_len+iov[2].iov_len; if (len != writev(new_fd, iov, 3)) { perror("writev"); return EXIT_FAILURE; } return recv_fd(new_fd, write); }