#include<unistd.h> #include <stdio.h> #include <stdlib.h> int main(int argec,char *argv[]){ int fd1[2],fd2[2]; pipe(fd1); pipe(fd2); int pid1=fork(); int pid2=fork(); if(pid1>0){ close(fd1[1]); close(fd2[1]); char str1[25]; char str2[25]; read(fd1[0],str1,25); printf("child1 say:%s\n",str1); read(fd2[0],str2,25); printf("child2 say:%s\n",str2); } if(pid1==0){ close(fd1[0]); char str1[30]; write(fd1[1],"hello father i am child1!",30); } if(pid2==0){ close(fd2[0]); char str2[30]; write(fd2[1],"hello father i am child2!",30); } return 0; }为什么这个代码运行后printf语句运行了两次
时间: 2024-01-20 19:02:12 浏览: 14
这个代码运行后,printf语句运行了两次是因为在父进程中,同时有两个子进程分别向管道fd1和fd2中写入了数据("hello father i am child1!"和"hello father i am child2!"),父进程在读取管道fd1和fd2中的数据时,会分别读取两个子进程写入的数据,因此printf语句会运行两次,分别输出"child1 say:hello father i am child1!"和"child2 say:hello father i am child2!"。
相关问题
#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的程序,可以列出文件的权限、链接数、所有者、所在组、文件大小、创建时间、文件名等信息。
#include <stdio.h> #include <linux/types.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <sys/ioctl.h> #include <string.h> #include <linux/i2c.h> #include <linux/i2c-dev.h> #include <stdio.h> #include <stdlib.h> #include <stdint.h> #define DEFAULT_I2C_BUS "/dev/i2c-0" /* PCF8591 应用层测试代码 */ int main(int argc,char **argv) { unsigned char data=0; int fp; float tmp; // tmp=5.34v 0.34 int a; int b; fp=open("/dev/Tiny4412_PCF8591",O_RDWR); if(fp<0) /*判断文件是否打开成功*/ { printf("PCF8591 driver open error!\n"); return -1; } while(1) { read(fp,&data,1); write(fp,&data,1); printf("ADC1=%d\n",data); tmp=(float)data*(5.0/255); //电压= 采集的数字量*(参考电压/分辨率); a=tmp; //a=5 tmp=5.3 b=(int)((tmp-a)*1000); //b=0.34 printf("ADC1=%d.%dV\r\n",(int)a,(int)b); sleep(1); } close(fp); return 0; }优化这段代码使其从PCF8591默认地址0x48里读取数据并输出数据
您好!根据您的要求,我帮您优化了代码,使其从PCF8591默认地址0x48中读取数据并输出。
```c
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#define DEFAULT_I2C_BUS "/dev/i2c-0"
#define PCF8591_ADDRESS 0x48
int main(int argc, char **argv)
{
int fp;
unsigned char data = 0;
fp = open(DEFAULT_I2C_BUS, O_RDWR);
if (fp < 0) {
printf("Failed to open I2C bus.\n");
return -1;
}
if (ioctl(fp, I2C_SLAVE, PCF8591_ADDRESS) < 0) {
printf("Failed to set I2C slave address.\n");
close(fp);
return -1;
}
while (1) {
if (read(fp, &data, 1) != 1) {
printf("Failed to read data from PCF8591.\n");
close(fp);
return -1;
}
printf("ADC1=%d\n", data);
float voltage = (float)data * (5.0 / 255);
int integerPart = (int)voltage;
int decimalPart = (int)((voltage - integerPart) * 1000);
printf("ADC1=%d.%dV\n", integerPart, decimalPart);
sleep(1);
}
close(fp);
return 0;
}
```
这样,代码就能从PCF8591默认地址0x48中读取数据,并输出读取到的数据。希望对您有帮助!如果还有其他问题,请随时提问。
阅读全文