Skip to content

Commit d08ddb7

Browse files
committed
Ch3 Exercises
1 parent 1b41296 commit d08ddb7

File tree

15 files changed

+334
-0
lines changed

15 files changed

+334
-0
lines changed

3-Process Concept/exercises/3.10.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <stdio.h>
2+
#include <unistd.h>
3+
#include <sys/types.h>
4+
5+
#define SIZE 5
6+
7+
int nums[SIZE] = {0,1,2,3,4};
8+
9+
int main()
10+
{
11+
int i;
12+
pid_t pid;
13+
14+
pid = fork();
15+
if(pid == 0){
16+
for(i=0; i<SIZE; i++){
17+
nums[i] *= -i;
18+
printf("Child %d\n", nums[i]);
19+
}
20+
}
21+
else if(pid > 0){
22+
wait(NULL);
23+
for (i = 0; i < SIZE; i++) {
24+
printf("Parent %d\n", nums[i]);
25+
}
26+
}
27+
28+
return 0;
29+
}

3-Process Concept/exercises/3.12.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <stdio.h>
2+
#include <unistd.h>
3+
#include <stdlib.h>
4+
#include <sys/types.h>
5+
6+
int main()
7+
{
8+
pid_t pid,pid1;
9+
10+
pid = fork();
11+
if(pid == 0){
12+
pid1 = getpid();
13+
printf("Child: pid = %d\n", pid1);
14+
15+
/* pass exit status */
16+
exit(2);
17+
}
18+
else if(pid > 0){
19+
// int status;
20+
// pid = wait(&status);
21+
22+
/* get parent pid */
23+
pid1 = getpid();
24+
printf("Parent: pid = %d\n", pid1);
25+
26+
/* execute ps -l from the parent */
27+
execlp("/bin/ps", "ps", "-l", NULL);
28+
29+
//execlp("/bin/kill", "kill", "-9", getppid(), NULL);
30+
//printf("Exit status: status = %d\n", status);
31+
}
32+
33+
return 0;
34+
}

3-Process Concept/exercises/3.14.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <stdio.h>
2+
#include <unistd.h>
3+
#include <sys/types.h>
4+
5+
int main()
6+
{
7+
int n;
8+
pid_t pid;
9+
10+
printf("Enter N:");
11+
scanf("%d", &n);
12+
13+
/* create child process */
14+
pid = fork();
15+
16+
if(pid == 0){ /* child process */
17+
while(n > 1){
18+
printf("%d, ", n);
19+
if(n%2 == 0) n = n/2;
20+
else n = 3*n + 1;
21+
}
22+
printf("%d\n", n);
23+
}
24+
else if(pid > 0){ /* parent process */
25+
wait(NULL);
26+
}
27+
28+
return 0;
29+
}

3-Process Concept/exercises/3.15.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <stdio.h>
2+
#include <unistd.h>
3+
#include <string.h>
4+
#include <stdlib.h>
5+
#include <fcntl.h>
6+
#include <sys/types.h>
7+
#include <sys/stat.h>
8+
#include <sys/mman.h>
9+
10+
int main()
11+
{
12+
/* size (in bytes) of shared memory object */
13+
const int SIZE = 8192;
14+
/* name of the shared memory object */
15+
const char *name = "OS";
16+
17+
/* shared memory file descriptor */
18+
int shm_fd;
19+
/* pointer to shared memory object */
20+
int *ptr;
21+
22+
int n;
23+
pid_t pid;
24+
25+
printf("Enter N:");
26+
scanf("%d", &n);
27+
28+
/* create the shared memory object */
29+
shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666);
30+
31+
/* create child process */
32+
pid = fork();
33+
34+
if(pid == 0){ /* child process */
35+
/* memory map the shared memory object */
36+
ptr = (int *)mmap(0, SIZE, PROT_WRITE, MAP_SHARED, shm_fd, 0);
37+
int i=0;
38+
while(n > 1){
39+
ptr[i++] = n;
40+
if(n%2 == 0) n = n/2;
41+
else n = 3*n + 1;
42+
}
43+
ptr[i] = n;
44+
}
45+
else if(pid > 0){ /* parent process */
46+
wait(NULL);
47+
/* memory map the shared memory object */
48+
ptr = mmap(0, SIZE, PROT_READ, MAP_SHARED, shm_fd, 0);
49+
int i=0;
50+
while(ptr[i]!=1){
51+
printf("%d,", ptr[i]);
52+
i++;
53+
}
54+
printf("%d\n", ptr[i]);
55+
}
56+
57+
return 0;
58+
}

3-Process Concept/exercises/3.5.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <stdio.h>
2+
#include <unistd.h>
3+
4+
int main()
5+
{
6+
pid_t pid;
7+
int i;
8+
int c=1;
9+
10+
for (i=0; i<2; i++){
11+
c++;
12+
pid=fork();
13+
}
14+
printf("PID %d\n", pid);
15+
if(pid != 0){
16+
wait(NULL);
17+
printf("%d\n", c);
18+
}
19+
return 0;
20+
}

3-Process Concept/exercises/3.8.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <stdio.h>
2+
#include <unistd.h>
3+
#include <sys/types.h>
4+
5+
int main()
6+
{
7+
pid_t pid, pid1;
8+
9+
/* fork a child process */
10+
pid = fork();
11+
12+
if(pid < 0){ /* error occured */
13+
fprintf(stderr, "Fork failed");
14+
return 1;
15+
}
16+
else if(pid == 0){ /* child process */
17+
pid1 = getpid();
18+
printf("child: pid = %d\n", pid);
19+
printf("child: pid1 = %d\n", pid1);
20+
}
21+
else{ /* parent process */
22+
pid1 = getpid();
23+
printf("parent: pid = %d\n", pid);
24+
printf("parent: pid1 = %d\n", pid1);
25+
wait(NULL);
26+
}
27+
28+
return 0;
29+
}
1.56 KB
Binary file not shown.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.net.*;
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class EchoClient{
6+
public static void main(String[] args){
7+
try {
8+
/* make connection to server socket */
9+
Socket socket = new Socket("127.0.0.1", 6010);
10+
11+
/* setup reading and writing to the socket */
12+
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
13+
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
14+
15+
InputStream in = socket.getInputStream();
16+
BufferedInputStream is = new BufferedInputStream(in);
17+
18+
/* byte array to store the input from BufferedInputStream */
19+
byte[] contents = new byte[1024];
20+
int len;
21+
String input;
22+
23+
/* keep running until null encountered */
24+
while( (input = reader.readLine()) != null){
25+
/* write string to socket */
26+
writer.write(input);
27+
writer.flush();
28+
29+
/* read string from socket */
30+
String line;
31+
len = is.read(contents);
32+
if(len == -1) break;
33+
line = new String(contents, 0, len);
34+
System.out.println(line);
35+
36+
/* exit on 'bye' */
37+
if(line.toLowerCase().contains("bye")) break;
38+
}
39+
40+
/* close the socket connection */
41+
socket.close();
42+
}catch(IOException ioe){
43+
System.err.println(ioe);
44+
}
45+
}
46+
}
1.48 KB
Binary file not shown.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.net.*;
2+
import java.io.*;
3+
4+
public class EchoServer{
5+
public static void main(String[] args){
6+
try{
7+
ServerSocket sock = new ServerSocket(6010);
8+
9+
/* listen for connections */
10+
while(true){
11+
Socket client = sock.accept();
12+
InputStream in = client.getInputStream();
13+
BufferedInputStream is = new BufferedInputStream(in);
14+
PrintWriter writer = new PrintWriter(client.getOutputStream(), true);
15+
16+
byte[] contents = new byte[1024];
17+
int len;
18+
19+
while(true){
20+
/* read input from socket */
21+
len = is.read(contents);
22+
23+
/* echo back to client */
24+
String line = new String(contents, 0, len);
25+
writer.write("Server: " + line);
26+
writer.flush();
27+
28+
if(line.toLowerCase().compareTo("bye")==0) break;
29+
}
30+
31+
/* close the socket and resume listening for connections */
32+
client.close();
33+
}
34+
}catch(IOException ioe){
35+
System.err.println(ioe);
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)