#include <stdio.h> #include <sys/types.h> #include <stdlib.h> #include <sys/stat.h> #include <fcntl.h> #include <error.h> #include <wait.h> #include <unistd.h> int main( ){ int pid1,pid2,pid3; int fd[2]; char outpipe[60],inpipe[60]; pipe(fd);//创建一个管道 while ((pid1=fork( ))==-1); printf("pid1=%d\n",pid1); if(pid1==0){ printf("The Child process 1 is sending message!\n"); lockf(fd[1],1,0);//互斥 sprintf(outpipe,"This is the child 1 process's message!\n"); write(fd[1],outpipe,60); sleep(1);//自我阻塞1秒,让出机会执行下一个进程,增加并发度 lockf(fd[1],0,0); exit(0); } else{ while((pid2=fork( ))==-1); printf("pid2=%d\n",pid2); if(pid2==0){ printf("The Child process 2 is sending message!\n"); lockf(fd[1],1,0); sprintf(outpipe,"This is the child 2 process's message!\n"); write(fd[1],outpipe,60); sleep(1); lockf(fd[1],0,0); exit(0); } else{ while((pid3=fork( ))==-1); printf("pid3=%d\n",pid3); if(pid3==0){ printf("The Child process 3 is sending message!\n"); lockf(fd[1],1,0); sprintf(outpipe,"This is the child 3 process's message!\n"); write(fd[1],outpipe,60); sleep(1); lockf(fd[1],0,0); exit(0); } else{ wait(0);//同步 read(fd[0],inpipe,60); printf("\n%s",inpipe); wait(0); read(fd[0],inpipe,60); printf("%s\n",inpipe); wait(0); read(fd[0],inpipe,60); printf("%s\n",inpipe); exit(0); } } } return 0; }(1)
这段代码是一个使用管道实现进程间通信的示例程序。它创建了三个子进程,每个子进程向管道中写入自己的消息,父进程从管道中读取这些消息并打印出来。
具体来说,首先通过 pipe(fd)
创建了一个管道,然后通过三次 fork()
分别创建了三个子进程。每个子进程都先使用 lockf()
函数对管道进行互斥,然后向管道中写入自己的消息,并使用 sleep()
函数自我阻塞 1 秒,让出机会执行下一个进程,从而增加并发度。最后再使用 lockf()
函数解除互斥并退出进程。
父进程则通过三次 wait()
函数等待子进程退出,并使用 read()
函数从管道中读取子进程写入的消息,并打印出来。
注意,这段代码中使用了固定的消息长度 60,这可能会导致消息被截断或者浪费空间。在实际使用中,可以通过动态分配内存来解决这个问题。
#include <stdio.h> #include <sys/types.h> #include <stdlib.h> #include <sys/stat.h> #include <fcntl.h> #include <error.h> #include <wait.h> #include <unistd.h> int main( ){ int pid1,pid2,pid3; int fd[2]; char outpipe[60],inpipe[60]; pipe(fd);//创建一个管道 whil
e((pid1=fork())==-1); //创建第一个子进程 if(pid1==0){ //子进程1的代码 close(fd[0]); //关闭读端 sprintf(outpipe,"Child process 1 is sending a message!"); write(fd[1],outpipe,60);//写入管道 exit(0);//子进程1退出 } else{ //父进程继续创建第二个子进程 while((pid2=fork())==-1); if(pid2==0){ //子进程2的代码 close(fd[1]);//关闭写端 read(fd[0],inpipe,60);//读取管道内容 printf("Child process 2 received a message: %s\n",inpipe); exit(0);//子进程2退出 } else{ //父进程继续创建第三个子进程 while((pid3=fork())==-1); if(pid3==0){ //子进程3的代码 close(fd[0]);//关闭读端 close(fd[1]);//关闭写端 printf("Child process 3 is running!\n"); exit(0);//子进程3退出 } else{ //父进程等待三个子进程结束 waitpid(pid1,NULL,0); waitpid(pid2,NULL,0); waitpid(pid3,NULL,0); printf("All child processes have finished!\n"); exit(0);//父进程退出 } } } return 0; }
这是一段使用管道实现进程间通信的程序,其中通过创建三个子进程,实现了进程间的消息传递和同步执行。第一个子进程向管道中写入一个消息,第二个子进程从管道中读取消息并输出,第三个子进程只是为了演示多进程的使用,没有实际作用。
以上是我的回答,是否能满足你的问题?
#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的程序,可以列出文件的权限、链接数、所有者、所在组、文件大小、创建时间、文件名等信息。
相关推荐
















