added more code
BIN
CPE435/Lab2/1642876381.png
Normal file
After Width: | Height: | Size: 13 KiB |
BIN
CPE435/Lab2/1642889504.png
Normal file
After Width: | Height: | Size: 9.6 KiB |
BIN
CPE435/Lab2/1642890159.png
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
CPE435/Lab2/1642890503.png
Normal file
After Width: | Height: | Size: 13 KiB |
BIN
CPE435/Lab2/1642892271.png
Normal file
After Width: | Height: | Size: 14 KiB |
BIN
CPE435/Lab2/Lab02.pdf
Normal file
BIN
CPE435/Lab2/Lab02_ex1Output.png
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
CPE435/Lab2/Lab2_ex1Output.png
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
CPE435/Lab2/Lab2_ex3Output.png
Normal file
After Width: | Height: | Size: 13 KiB |
BIN
CPE435/Lab2/Lab2_ex4Output.png
Normal file
After Width: | Height: | Size: 9.6 KiB |
BIN
CPE435/Lab2/Report 2.pdf
Normal file
BIN
CPE435/Lab2/a.out
Executable file
BIN
CPE435/Lab2/lab2_ex1
Executable file
23
CPE435/Lab2/lab2_ex1.cpp
Normal file
@ -0,0 +1,23 @@
|
||||
using namespace std;
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
int val = 0;
|
||||
int pid;
|
||||
pid = fork();
|
||||
if (pid == 0 ) /* we are the child */
|
||||
{
|
||||
val=+2;
|
||||
cout << "Id: " << getpid() << "\tVal: " << val << endl;
|
||||
}
|
||||
else //(getpid > 0) // we are the parent
|
||||
{
|
||||
val=+5;
|
||||
cout << "Id: " << getpid() << "\tVal: " << val << endl;
|
||||
}
|
||||
}
|
BIN
CPE435/Lab2/lab2_ex2
Executable file
61
CPE435/Lab2/lab2_ex2.cpp
Normal file
@ -0,0 +1,61 @@
|
||||
using namespace std;
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
void mult(int x, int y, int id);
|
||||
|
||||
void sub(int x, int y, int id);
|
||||
|
||||
void add(int x, int y, int id);
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
int pid;
|
||||
cout << "Parent's PID: " << getpid() << endl;
|
||||
pid = fork();
|
||||
if (pid == 0 ) // child1
|
||||
{
|
||||
int pidChild2;
|
||||
sub(4, 5, getpid());
|
||||
|
||||
// child1 subtracts numbers
|
||||
pidChild2 = fork(); // child2
|
||||
if (pidChild2 == 0)
|
||||
{
|
||||
add(4, 5, getpid());
|
||||
exit(0);
|
||||
}
|
||||
else // child1
|
||||
{
|
||||
wait(0);
|
||||
mult(4, 5, getpid());
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
else // parent of both
|
||||
{
|
||||
wait(0);
|
||||
}
|
||||
}
|
||||
|
||||
void mult(int x, int y, int id)
|
||||
{
|
||||
cout << "4*5 = " << x*y << endl;
|
||||
cout << "Child1's ID: " << id << endl;
|
||||
}
|
||||
|
||||
void sub(int x, int y, int id)
|
||||
{
|
||||
cout << "4-5 = " << x-y << endl;
|
||||
cout << "Child1's ID: " << id << endl;
|
||||
}
|
||||
|
||||
|
||||
void add(int x, int y, int id)
|
||||
{
|
||||
cout << "4+5 = " << x+y << endl;
|
||||
cout << "Child2's ID: " << id << endl;
|
||||
}
|
BIN
CPE435/Lab2/lab2_ex3
Executable file
28
CPE435/Lab2/lab2_ex3.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
using namespace std;
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
int proc;
|
||||
int x;
|
||||
cout << "Please enter a number of processes to spawn: ";
|
||||
cin >> proc;
|
||||
if((proc % 2) != 0)
|
||||
{
|
||||
cout << "Odd number entered. Please enter an even number. Exiting now." << endl;
|
||||
return(0);
|
||||
}
|
||||
|
||||
for(int i=0; i<proc; i++){
|
||||
x = fork();
|
||||
if (x == 0 ) /* we are the child */
|
||||
{
|
||||
cout << "Id: " << getpid() << endl;
|
||||
exit(0);
|
||||
}
|
||||
// cout << "I am the parent, the child's pid is " << x << endl;
|
||||
}
|
||||
//exit(0);
|
||||
}
|
9
CPE435/Lab2/lab2_ex4.cpp
Normal file
@ -0,0 +1,9 @@
|
||||
using namespace std;
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
|
||||
}
|
BIN
CPE435/Lab2/lab2_ex4_orphan
Executable file
22
CPE435/Lab2/lab2_ex4_orphan.cpp
Normal file
@ -0,0 +1,22 @@
|
||||
using namespace std;
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
int pid;
|
||||
pid = fork();
|
||||
if (pid == 0) {
|
||||
printf("I am the child, my process id is %d\n",getpid( ));
|
||||
printf("the child’s parent process id is %d\n",getppid( ));
|
||||
sleep(20);
|
||||
printf("I am the child, my process id is %d\n",getpid( ));
|
||||
printf("I am the child, parent's process id is %d\n",getppid( ));
|
||||
} else
|
||||
{
|
||||
printf("I am the parent, my process id is %d\n",getpid( ));
|
||||
printf("the parents parent process id is %d\n",getppid( ));
|
||||
}
|
||||
}
|
BIN
CPE435/Lab2/lab2_ex4_sleeping
Executable file
11
CPE435/Lab2/lab2_ex4_sleeping.cpp
Normal file
@ -0,0 +1,11 @@
|
||||
using namespace std;
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
sleep(50);
|
||||
printf("Hello World");
|
||||
}
|
BIN
CPE435/Lab2/lab2_ex4_zombie
Executable file
14
CPE435/Lab2/lab2_ex4_zombie.cpp
Normal file
@ -0,0 +1,14 @@
|
||||
using namespace std;
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
if (fork ( ) > 0)
|
||||
{
|
||||
printf("parent\n");
|
||||
sleep(50);
|
||||
}
|
||||
}
|
BIN
CPE435/Lab2/out
Executable file
BIN
CPE435/Lab2/sleeping.png
Normal file
After Width: | Height: | Size: 14 KiB |
BIN
CPE435/Lab2/zombie.png
Normal file
After Width: | Height: | Size: 19 KiB |