initial push of CPU API code
This commit is contained in:
18
cpu-api/Makefile
Normal file
18
cpu-api/Makefile
Normal file
@@ -0,0 +1,18 @@
|
||||
|
||||
all: p1 p2 p3 p4
|
||||
|
||||
clean:
|
||||
rm -f p1 p2 p3 p4
|
||||
|
||||
p1: p1.c
|
||||
gcc -o p1 p1.c -Wall
|
||||
|
||||
p2: p2.c
|
||||
gcc -o p2 p2.c -Wall
|
||||
|
||||
p3: p3.c
|
||||
gcc -o p3 p3.c -Wall
|
||||
|
||||
p4: p4.c
|
||||
gcc -o p4 p4.c -Wall
|
||||
|
||||
8
cpu-api/README.md
Normal file
8
cpu-api/README.md
Normal file
@@ -0,0 +1,8 @@
|
||||
|
||||
To compile, just type "make".
|
||||
|
||||
See the highly primitive "Makefile" for details.
|
||||
|
||||
Then run p1, p2, p3, or p4, as need be.
|
||||
|
||||
|
||||
23
cpu-api/p1.c
Normal file
23
cpu-api/p1.c
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
printf("hello world (pid:%d)\n", (int) getpid());
|
||||
int rc = fork();
|
||||
if (rc < 0) {
|
||||
// fork failed; exit
|
||||
fprintf(stderr, "fork failed\n");
|
||||
exit(1);
|
||||
} else if (rc == 0) {
|
||||
// child (new process)
|
||||
printf("hello, I am child (pid:%d)\n", (int) getpid());
|
||||
} else {
|
||||
// parent goes down this path (original process)
|
||||
printf("hello, I am parent of %d (pid:%d)\n",
|
||||
rc, (int) getpid());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
26
cpu-api/p2.c
Normal file
26
cpu-api/p2.c
Normal file
@@ -0,0 +1,26 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
printf("hello world (pid:%d)\n", (int) getpid());
|
||||
int rc = fork();
|
||||
if (rc < 0) {
|
||||
// fork failed; exit
|
||||
fprintf(stderr, "fork failed\n");
|
||||
exit(1);
|
||||
} else if (rc == 0) {
|
||||
// child (new process)
|
||||
printf("hello, I am child (pid:%d)\n", (int) getpid());
|
||||
sleep(1);
|
||||
} else {
|
||||
// parent goes down this path (original process)
|
||||
int wc = wait(NULL);
|
||||
printf("hello, I am parent of %d (wc:%d) (pid:%d)\n",
|
||||
rc, wc, (int) getpid());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
32
cpu-api/p3.c
Normal file
32
cpu-api/p3.c
Normal file
@@ -0,0 +1,32 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
printf("hello world (pid:%d)\n", (int) getpid());
|
||||
int rc = fork();
|
||||
if (rc < 0) {
|
||||
// fork failed; exit
|
||||
fprintf(stderr, "fork failed\n");
|
||||
exit(1);
|
||||
} else if (rc == 0) {
|
||||
// child (new process)
|
||||
printf("hello, I am child (pid:%d)\n", (int) getpid());
|
||||
char *myargs[3];
|
||||
myargs[0] = strdup("wc"); // program: "wc" (word count)
|
||||
myargs[1] = strdup("p3.c"); // argument: file to count
|
||||
myargs[2] = NULL; // marks end of array
|
||||
execvp(myargs[0], myargs); // runs word count
|
||||
printf("this shouldn't print out");
|
||||
} else {
|
||||
// parent goes down this path (original process)
|
||||
int wc = wait(NULL);
|
||||
printf("hello, I am parent of %d (wc:%d) (pid:%d)\n",
|
||||
rc, wc, (int) getpid());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
34
cpu-api/p4.c
Normal file
34
cpu-api/p4.c
Normal file
@@ -0,0 +1,34 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <assert.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int rc = fork();
|
||||
if (rc < 0) {
|
||||
// fork failed; exit
|
||||
fprintf(stderr, "fork failed\n");
|
||||
exit(1);
|
||||
} else if (rc == 0) {
|
||||
// child: redirect standard output to a file
|
||||
close(STDOUT_FILENO);
|
||||
open("./p4.output", O_CREAT|O_WRONLY|O_TRUNC, S_IRWXU);
|
||||
|
||||
// now exec "wc"...
|
||||
char *myargs[3];
|
||||
myargs[0] = strdup("wc"); // program: "wc" (word count)
|
||||
myargs[1] = strdup("p4.c"); // argument: file to count
|
||||
myargs[2] = NULL; // marks end of array
|
||||
execvp(myargs[0], myargs); // runs word count
|
||||
} else {
|
||||
// parent goes down this path (original process)
|
||||
int wc = wait(NULL);
|
||||
assert(wc >= 0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user