int main()
{
char buf[20];
int piledes[2];
pid_t pid;
int ret;
if(ret = pipe(piledes) == -1)
{
perror(\"error on pipe:\");
exit(-1);
}
else
{
pid = fork();
if(pid < 0)
{
perror(\"error on fork:\");
exit(-1);
}
else if(pid == 0)
{
close(piledes[0]);
printf(\"to fu:\");
fgets(buf,sizeof(buf)-1,stdin);
if((ret = write(piledes[1],buf,strlen(buf))) < 0)
{
perror(\"error on writing:\");
exit(-1);
}
close(piledes[1]);
}
else
{
close(piledes[1]);
if((ret = read(piledes[0],buf,sizeof(buf)-1)) < 0)
{
perror(\"error on reading:\");
exit(-1);
}
buf[ret] = '\\0';
printf(\"from zi:%s\\n\
close(piledes[0]);
}
}
return 0;
}
2、编写一个 C语言程序lswc,使之功能能和ls | wc 等价。(也就是利用,fork,exec,以及ls,wc,以及重定向,或者是管道的系统调用) 13’
#include #include #include #include #include #include #include #include #include #include #include #include #include #include int main(int argc,char *argv[])
{
int pildes[2];
int ret;
char *argument[3];
pid_t pid;
char buf[1000];
if(argc < 2)
{
printf(\"que shao can shu\\n\");
exit(0);
}
argument[0] = \"ls\";
argument[1] = \"/\";
argument[2] = NULL;
if((ret = pipe(pildes)) < 0)
{
perror(\"error on pipe:\");
exit(-1);
}
pid = fork();
if(pid <0)
{
printf(\"fork error\\n\");
exit(-1);
}
if(pid == 0)
{
close(pildes[0]);
dup2(pildes[1],1);
execvp(\"ls\
close(pildes[1]);
}
else
{
close(pildes[1]);
// dup2(fildes[0],0);
if(strcmp((argv[1]),\"wc\") == 0)
{
bzero(buf,sizeof(buf));
if((ret = read(pildes[0],buf,sizeof(buf))) < 0)
{
printf(\"error on reading:\");
exit(-1);
}
buf[ret] = '\\0';
printf(\"%s\
close(pildes[0]);
}
}
return 0;
}
3、实现一个具有头节点的链表。要求具有create, insert, delete, search功能,能对链表。
编写一个应用程序,使用上面的函数。 10'
#include #include #include #include #define PASSWD 111111
struct student
{
int num;
char name[20];
float grade;
struct student *next;
};
typedef struct student stuNode;
typedef struct student *stuPtr;
int stuwrite(stuPtr head)
{
FILE *fp;
fp = fopen(\"stu\
while(head->next != NULL)
{
head = head->next;
fwrite(head,sizeof(stuNode),1,fp);
}
close(fp);
return 0;
}
stuPtr sturead()
{
FILE *fp;
stuPtr head,p1,p2;
int a;
head = (stuPtr)malloc(sizeof(stuNode));
head->next = NULL;
p1 = head;
fp = fopen(\"stu\
do
{
p2 = (stuPtr)malloc(sizeof(stuNode));
p2->next = NULL;
a = fread(head,sizeof(stuNode),1,fp);
p1->next = p2;
p1 = p2;
}while(a == 1);
fclose(fp);
return head;
}
void creat()
{
stuPtr head,p;
head = (stuPtr)malloc(sizeof(stuNode));
p = (stuPtr)malloc(sizeof(stuNode));
p->next = NULL;
head->next = p;
printf(\"学号:\");
scanf(\"%d\
getchar();
printf(\"姓名:\");
scanf(\"%s\
getchar();
printf(\"成绩:\");
scanf(\"%f\
getchar();
stuwrite(head);
}
stuPtr insert(stuPtr head)
{
stuPtr p,q;
p = (stuPtr)malloc(sizeof(stuNode));
printf(\"学号:\");
scanf(\"%d\
getchar();
printf(\"姓名:\");
scanf(\"%s\
getchar();
printf(\"成绩:\");
scanf(\"%f\
getchar();
q = head;
while(q->next != NULL && p->num > q->next->num)
{
q = q->next;
}
p->next = q->next;
q->next = p;
stuwrite(head);
p = sturead();
return p;
}
stuPtr search(stuPtr head,int num)
{
stuPtr p1,p2;
int flag;
p1 = (stuPtr)malloc(sizeof(stuNode));
p2 = (stuPtr)malloc(sizeof(stuNode));
p1 = head;
while(p1->next != NULL)
{
flag = 0;
p2 = p1;
p1 = p1->next;
if(p1->num == num)
{
flag = 1;
break;
}
}
if(flag == 1)
{
p1 = p2;
return p1;
}
else
{
printf(\"没有此人\\n\");
return NULL;
}
}
stuPtr delete(stuPtr head, int num)
{
stuPtr p1,p2;
int flag;
p1 = search(head,num);
p2 = p1->next;
p1->next = p2->next;
free(p2);
stuwrite(head);
p1 = sturead();
return p1;
}
void print(stuPtr head)
{
stuPtr p;
int i;
printf(\"学号\姓名\成绩\\n\");
p = head->next;
while(p != NULL)
{
if(p->num >0 && p->grade >0)
{
printf(\"%d\%s\%.1f\\n\
}
p = p->next;
}
}
void print1(stuPtr p)
{
printf(\"学号\姓名\成绩\\n\");
while(p != NULL)
{
if(p->num >0 && p->grade >0)
{
printf(\"%d\%s\%.1f\\n\
}
p = p->next;
}
}
int main()
{
int choose,num;
stuPtr head,p;
head = (stuPtr)malloc(sizeof(stuNode));
p = (stuPtr)malloc(sizeof(stuNode));
while(1)
{
printf(\"1\创建链表\\n2\添加节点\\n3\删除节点\\n4\查找学生\\n5\查看所有学生信息\\n0\退出\\n\");
scanf(\"%d\
switch(choose)
{
case 1:
creat();
break;
case 2:
head = sturead();
insert(head);
break;
case 3:
head = sturead();
printf(\"删除学生的学号:\");
scanf(\"%d\
delete(head,num);
break;
case 4:
head = sturead();
printf(\"学生学号:\");
scanf(\"%d\
p = search(head,num);
p = p->next;
print1(p);
break;
case 5:
head = sturead();
print(head);
case 0:
break;
default:
break;
}
}
return 0;
}