详细分析以下代码#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/wait.h> int main() { int i, r, p1, p2, fd[2]; char buf[50], s[50]; if (pipe(fd) < 0) { perror("pipe error"); exit(1); } while ((p1 = fork()) == -1); if (p1 == 0) { close(fd[0]); sprintf(buf, "Child process P1 is sending message!\n"); printf("Child process P1 is sending message!\n"); write(fd[1], buf, 50); sleep(1); close(fd[1]); exit(0); } else { while ((p2 = fork()) == -1); if (p2 == 0) { close(fd[0]); sprintf(buf, "Child process P2 is sending message!\n"); printf("Child process P2 is sending message!\n"); write(fd[1], buf, 50); sleep(1); close(fd[1]); exit(0); } close(fd[1]); wait(0); if ((r = read(fd[0], s, 50)) == -1) printf("can't read pipe.\n"); else printf("Parent process is reading (%s) \n", s); wait(0); if ((r = read(fd[0], s, 50)) == -1) printf("can't read pipe.\n"); else printf("Parent process is reading (%s) \n", s); close(fd[0]); } return 0; }
时间: 2024-02-02 22:03:05 浏览: 83
这是一个使用管道实现进程间通信的示例代码。程序中使用了两个子进程p1和p2,以及一个父进程。父进程创建了一个管道fd,然后创建两个子进程p1和p2。子进程p1和p2分别向管道写入一条消息,然后关闭管道写入端。父进程阻塞等待两个子进程结束后,从管道读取两条消息,分别输出到屏幕上。
具体来说,代码运行流程如下:
1. 创建一个管道fd。
2. 父进程创建子进程p1,p1向管道写入一条消息,然后关闭管道写入端。
3. 父进程创建子进程p2,p2向管道写入一条消息,然后关闭管道写入端。
4. 父进程阻塞等待子进程p1和p2结束。
5. 父进程从管道读取一条消息,输出到屏幕上。
6. 父进程从管道读取另一条消息,输出到屏幕上。
7. 关闭管道读取端,程序结束。
在程序中,管道的读写操作使用了系统调用函数write和read。在子进程中,write函数用于向管道写入数据,read函数用于从管道读取数据;在父进程中,read函数用于从管道读取数据,write函数用于向管道写入数据。通过这种方式,进程之间就可以进行通信了。
相关问题
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <dirent.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <pwd.h> #include <grp.h> #include <time.h> void shell_ls_l(char *file,struct stat st) { char limi
ted[11]; // 文件权限 mode_t mode = st.st_mode; limitted[0] = (S_ISDIR(mode)) ? "d" : "-"; limitted[1] = (mode & S_IRUSR) ? "r" : "-"; limitted[2] = (mode & S_IWUSR) ? "w" : "-"; limitted[3] = (mode & S_IXUSR) ? "x" : "-"; limitted[4] = (mode & S_IRGRP) ? "r" : "-"; limitted[5] = (mode & S_IWGRP) ? "w" : "-"; limitted[6] = (mode & S_IXGRP) ? "x" : "-"; limitted[7] = (mode & S_IROTH) ? "r" : "-"; limitted[8] = (mode & S_IWOTH) ? "w" : "-"; limitted[9] = (mode & S_IXOTH) ? "x" : "-"; limitted[10] = '\0'; printf("%s ", limitted); // 链接数、所有者、所在组、文件大小、创建时间、文件名 printf("%ld ", (long)st.st_nlink); printf("%s ", getpwuid(st.st_uid)->pw_name); printf("%s ", getgrgid(st.st_gid)->gr_name); printf("%lld ", (long long)st.st_size); char time_buf[20]; strftime(time_buf, 20, "%b %d %H:%M", localtime(&st.st_ctime)); printf("%s ", time_buf); printf("%s\n", file); } int main(int argc, char *argv[]) { DIR *dir; struct dirent *ptr; struct stat st; char path[256]; if (argc == 1) { getcwd(path, 256); dir = opendir(path); while ((ptr = readdir(dir)) != NULL) { if (ptr->d_name[0] == '.') continue; stat(ptr->d_name, &st); shell_ls_l(ptr->d_name, st); } } else if (argc == 2) { if (strcmp(argv[1], "-l") == 0) { getcwd(path, 256); dir = opendir(path); while ((ptr = readdir(dir)) != NULL) { if (ptr->d_name[0] == '.') continue; stat(ptr->d_name, &st); shell_ls_l(ptr->d_name, st); } } else { dir = opendir(argv[1]); while ((ptr = readdir(dir)) != NULL) { if (ptr->d_name[0] == '.') continue; sprintf(path, "%s/%s", argv[1], ptr->d_name); stat(path, &st); shell_ls_l(ptr->d_name, st); } } } else if (argc == 3) { if (strcmp(argv[1], "-l") == 0) { dir = opendir(argv[2]); while ((ptr = readdir(dir)) != NULL) { if (ptr->d_name[0] == '.') continue; sprintf(path, "%s/%s", argv[2], ptr->d_name); stat(path, &st); shell_ls_l(ptr->d_name, st); } } } return 0; }
这是一个简单的实现Linux命令ls -l的程序,可以列出文件的权限、链接数、所有者、所在组、文件大小、创建时间、文件名等信息。
/* Socket head file */ #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h>/*sockaddr_in{}*/ #include<arpa/inet.h> /* Unix standard head file */ #include<stdio.h> #include<stdlib.h> #include<string.h> #include<unistd.h> #include <errno.h>/* strerror(),perror(),errno head file*/ #include <sys/time.h> #include <sys/wait.h> #define MAXBUFF 1024 #define PORT 9999怎么改成windows系统
在Windows系统中,网络编程需要使用Winsock API,因此需要引入`<winsock2.h>`头文件。此外,需要在程序中调用`WSAStartup()`函数和`WSACleanup()`函数来初始化和释放Winsock库。
下面是在Windows系统下进行网络编程的头文件和常量定义示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")
#define MAXBUFF 1024
#define PORT 9999
```
其中,`<ws2tcpip.h>`头文件包含了一些网络编程相关的函数和结构体定义。`#pragma comment(lib, "Ws2_32.lib")`语句用于链接Winsock库。
需要注意的是,在Windows系统中,端口号的类型为`unsigned short`,而不是`int`。因此,定义端口号常量时应该使用`USHORT`类型:
```c
#define PORT (USHORT)9999
```
阅读全文
相关推荐

















