parent
05f0efd3fd
commit
aec8667eb0
Binary file not shown.
@ -0,0 +1,32 @@
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
int child, grandson;
|
||||
char *sargs[] = { "/bin/echo", "Son ", "Hello", "World!", NULL};
|
||||
char *gargs[] = { "/bin/echo", "Grandson ", "Hello", "World!", NULL};
|
||||
|
||||
if (!(child = fork()))
|
||||
{
|
||||
/* child */
|
||||
printf("child:s1: pid %d: %d is my father\n", getpid(), getppid());
|
||||
if(!(grandson=fork()))
|
||||
{
|
||||
printf("grandson: pid %d: %d is my father\n", getpid(), getppid());
|
||||
execve("/bin/echo", gargs, NULL);
|
||||
printf("pid %d: I am back, something is wrong!\n", getpid());
|
||||
}else{
|
||||
printf("child:s2: pid %d: %d is my father\n", getpid(), getppid());
|
||||
wait4(grandson, NULL, 0, NULL);
|
||||
execve("/bin/echo", sargs, NULL);
|
||||
printf("pid %d: I am back, something is wrong!\n", getpid());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
int myself = getpid();
|
||||
printf("parent: pid %d: %d is my son\n", myself, child);
|
||||
wait4(child, NULL, 0, NULL);
|
||||
printf("pid %d: done\n", myself);
|
||||
}
|
||||
return 0;
|
||||
}
|
Binary file not shown.
Binary file not shown.
@ -0,0 +1,38 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define DEPTH 4
|
||||
#define SLEEP_TIME 1
|
||||
void forktree(const char *cur);
|
||||
|
||||
void
|
||||
forkchild(const char *cur, char branch) {
|
||||
char nxt[DEPTH + 1];
|
||||
|
||||
if (strlen(cur) >= DEPTH)
|
||||
return;
|
||||
|
||||
snprintf(nxt, DEPTH + 1, "%s%c", cur, branch);
|
||||
if (fork() == 0) {
|
||||
forktree(nxt);
|
||||
//yield();
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
forktree(const char *cur) {
|
||||
printf("%04x: I am '%s'\n", getpid(), cur);
|
||||
|
||||
forkchild(cur, '0');
|
||||
forkchild(cur, '1');
|
||||
}
|
||||
|
||||
int
|
||||
main(void) {
|
||||
printf("forktree process will sleep %d ticks\n",SLEEP_TIME);
|
||||
sleep(SLEEP_TIME);
|
||||
forktree("");
|
||||
return 0;
|
||||
}
|
||||
|
Binary file not shown.
@ -0,0 +1,52 @@
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/uio.h>
|
||||
char head[1024],foo[48], bar[51], baz[49];
|
||||
struct iovec iov[3];
|
||||
int main ()
|
||||
{
|
||||
|
||||
|
||||
ssize_t nr;
|
||||
int fd, i;
|
||||
|
||||
fd = open ("hello.sh", O_RDONLY);
|
||||
if (fd==-1){
|
||||
perror ("open");
|
||||
return 1;
|
||||
}
|
||||
nr=read(fd, head, sizeof(head));
|
||||
if(nr==-1) {
|
||||
perror("read");
|
||||
return 2;
|
||||
}
|
||||
|
||||
printf ("read size %d, head: %s\n", nr, (char *) head);
|
||||
|
||||
/* set up our iovec structures */
|
||||
iov[0].iov_base = foo;
|
||||
iov[0].iov_len = sizeof (foo);
|
||||
iov[1].iov_base = bar;
|
||||
iov[1].iov_len = sizeof (bar);
|
||||
iov[2].iov_base = baz;
|
||||
iov[2].iov_len = sizeof (baz);
|
||||
|
||||
/* read into the structures with a single call */
|
||||
nr = readv (fd, iov, 3);
|
||||
if (nr==-1) {
|
||||
perror ("readv");
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (i = 0; i < 3; i++)
|
||||
printf ("IOV%d: size:%d %s\n", i, nr, (char *) iov[i].iov_base);
|
||||
|
||||
if (close (fd)) {
|
||||
perror ("close");
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in new issue