added more code
16
CPE435/.vscode/c_cpp_properties.json
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
{
|
||||
"configurations": [
|
||||
{
|
||||
"name": "linux-gcc-x64",
|
||||
"includePath": [
|
||||
"${workspaceFolder}/**"
|
||||
],
|
||||
"compilerPath": "gcc",
|
||||
"cStandard": "${default}",
|
||||
"cppStandard": "${default}",
|
||||
"intelliSenseMode": "linux-gcc-x64",
|
||||
"compilerArgs": []
|
||||
}
|
||||
],
|
||||
"version": 4
|
||||
}
|
5
CPE435/.vscode/extensions.json
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"recommendations": [
|
||||
"ms-vscode.cpptools"
|
||||
]
|
||||
}
|
49
CPE435/.vscode/launch.json
vendored
Normal file
@ -0,0 +1,49 @@
|
||||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "gcc - Build and debug active file",
|
||||
"type": "cppdbg",
|
||||
"request": "launch",
|
||||
"program": "${fileDirname}/${fileBasenameNoExtension}",
|
||||
"args": [],
|
||||
"stopAtEntry": false,
|
||||
"cwd": "${fileDirname}",
|
||||
"environment": [],
|
||||
"externalConsole": false,
|
||||
"MIMode": "gdb",
|
||||
"setupCommands": [
|
||||
{
|
||||
"description": "Enable pretty-printing for gdb",
|
||||
"text": "-enable-pretty-printing",
|
||||
"ignoreFailures": true
|
||||
}
|
||||
],
|
||||
"preLaunchTask": "C/C++: g++ build active file",
|
||||
"miDebuggerPath": "/usr/bin/gdb"
|
||||
},
|
||||
{
|
||||
"name": "C/C++ Runner: Debug Session",
|
||||
"type": "cppdbg",
|
||||
"request": "launch",
|
||||
"args": [
|
||||
""
|
||||
],
|
||||
"stopAtEntry": false,
|
||||
"cwd": "/home/student/anw0044/CPE435Lab_SP22/Lab6",
|
||||
"environment": [],
|
||||
"program": "/home/student/anw0044/CPE435Lab_SP22/Lab6/build/Debug/outDebug",
|
||||
"internalConsoleOptions": "openOnSessionStart",
|
||||
"MIMode": "gdb",
|
||||
"miDebuggerPath": "/usr/bin/gdb",
|
||||
"externalConsole": false,
|
||||
"setupCommands": [
|
||||
{
|
||||
"description": "Enable pretty-printing for gdb",
|
||||
"text": "-enable-pretty-printing",
|
||||
"ignoreFailures": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
38
CPE435/.vscode/settings.json
vendored
Normal file
@ -0,0 +1,38 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"ostream": "cpp",
|
||||
"iostream": "cpp",
|
||||
"wait.h": "c",
|
||||
"stdio.h": "c",
|
||||
"*.tcc": "cpp"
|
||||
},
|
||||
"cmake.configureOnOpen": false,
|
||||
"C_Cpp_Runner.cCompilerPath": "gcc",
|
||||
"C_Cpp_Runner.cppCompilerPath": "/usr/bin/g++",
|
||||
"C_Cpp_Runner.debuggerPath": "/usr/bin/gdb",
|
||||
"C_Cpp_Runner.cStandard": "",
|
||||
"C_Cpp_Runner.cppStandard": "",
|
||||
"C_Cpp_Runner.msvcBatchPath": "",
|
||||
"C_Cpp_Runner.warnings": [
|
||||
"-Wall",
|
||||
"-Wextra",
|
||||
"-Wpedantic"
|
||||
],
|
||||
"C_Cpp_Runner.enableWarnings": true,
|
||||
"C_Cpp_Runner.warningsAsError": false,
|
||||
"C_Cpp_Runner.compilerArgs": [],
|
||||
"C_Cpp_Runner.linkerArgs": [],
|
||||
"C_Cpp_Runner.includePaths": [],
|
||||
"C_Cpp_Runner.includeSearch": [
|
||||
"*",
|
||||
"**/*"
|
||||
],
|
||||
"C_Cpp_Runner.excludeSearch": [
|
||||
"**/build",
|
||||
"**/build/**",
|
||||
"**/.*",
|
||||
"**/.*/**",
|
||||
"**/.vscode",
|
||||
"**/.vscode/**"
|
||||
]
|
||||
}
|
26
CPE435/.vscode/tasks.json
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
{
|
||||
"tasks": [
|
||||
{
|
||||
"type": "cppbuild",
|
||||
"label": "C/C++: g++ build active file",
|
||||
"command": "/usr/bin/gcc",
|
||||
"args": [
|
||||
"-lm",
|
||||
"-fdiagnostics-color=always",
|
||||
"-g",
|
||||
"${file}",
|
||||
"-o",
|
||||
"${fileDirname}/${fileBasenameNoExtension}"
|
||||
],
|
||||
"options": {
|
||||
"cwd": "${fileDirname}"
|
||||
},
|
||||
"problemMatcher": [
|
||||
"$gcc"
|
||||
],
|
||||
"group": "build",
|
||||
"detail": "compiler: /usr/bin/gcc"
|
||||
},
|
||||
|
||||
]
|
||||
}
|
BIN
CPE435/Lab1/Lab01.pdf
Normal file
BIN
CPE435/Lab1/Lab01_Study_Material.pdf
Normal file
BIN
CPE435/Lab1/demo_1
Executable file
19
CPE435/Lab1/demo_1.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
using namespace std;
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
pid_t c_pid;
|
||||
cout << "The pid of the parent is " << getpid() << endl;
|
||||
c_pid = fork();
|
||||
if (c_pid == 0 ) /* we are the child */
|
||||
{
|
||||
cout << "I am the child, my parents pid was " << getppid() << endl;
|
||||
exit(0);
|
||||
}
|
||||
cout << "I am the parent, the child's pid is " << c_pid << endl;
|
||||
|
||||
exit(0);
|
||||
}
|
BIN
CPE435/Lab1/demo_2
Executable file
17
CPE435/Lab1/demo_2.cpp
Normal file
@ -0,0 +1,17 @@
|
||||
/* This program illustrates the use of fork */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <unistd.h>
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int x;
|
||||
x = 0;
|
||||
fork();
|
||||
x++;
|
||||
// This should be printed twice, once by the parent and once by the child
|
||||
cout << "I am process "<< getpid() << " and my x is " << x << endl;
|
||||
return 0;
|
||||
}
|
BIN
CPE435/Lab1/demo_3
Executable file
18
CPE435/Lab1/demo_3.cpp
Normal file
@ -0,0 +1,18 @@
|
||||
/* This program illustrates multiple fork operations */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("I am the Parent\n");
|
||||
fork();
|
||||
printf("This is printed by both parent and child\n");
|
||||
fork();
|
||||
printf("This will be printed 4 times\n");
|
||||
return 0;
|
||||
}
|
BIN
CPE435/Lab1/demo_4
Executable file
25
CPE435/Lab1/demo_4.cpp
Normal file
@ -0,0 +1,25 @@
|
||||
/* This program illustrates the death of the parent process before the child is terminated. */
|
||||
/* The child process is now considered an orphan process since the parent is dead. */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int pid;
|
||||
pid = fork();
|
||||
if (pid == 0)
|
||||
{
|
||||
printf("I am the child, my ID is %d\n", getpid());
|
||||
printf("I am the child, my parent is %d\n", getppid());
|
||||
printf("The child will now sleep for 10 seconds\n");
|
||||
sleep(10);
|
||||
printf("I am the same child with ID %d, but my parent Id is %d\n", getpid(), getppid());
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("I am the parent with ID %d. My parent is %d and my child is %d\n", getpid(), getppid(), pid);
|
||||
sleep(5);
|
||||
}
|
||||
}
|
BIN
CPE435/Lab1/lab1
Executable file
20
CPE435/Lab1/lab1.cpp
Normal file
@ -0,0 +1,20 @@
|
||||
using namespace std;
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
pid_t x;
|
||||
cout << "The pid of the parent is " << getpid() << endl;
|
||||
for(int i=0; i<10; i++){
|
||||
x = fork();
|
||||
if (x == 0 ) /* we are the child */
|
||||
{
|
||||
cout << "Id: " << getpid() << "\tX: x "<< endl;
|
||||
exit(0);
|
||||
}
|
||||
// cout << "I am the parent, the child's pid is " << x << endl;
|
||||
}
|
||||
exit(0);
|
||||
}
|
3
CPE435/Lab1/lab1Questions.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Question 1: The processes' PIDs are different each time the program is run, and both processes differ by 1.
|
||||
2: Four are running.
|
||||
3:
|
BIN
CPE435/Lab10/Lab10.pdf
Normal file
BIN
CPE435/Lab10/UFTP_v3_transfer.pcapng
Normal file
BIN
CPE435/Lab10/apache_pb.png
Normal file
After Width: | Height: | Size: 1.4 KiB |
BIN
CPE435/Lab10/lab10Capture.pcap
Normal file
BIN
CPE435/Lab10/lab10CaptureOpenOffice.pcap
Normal file
BIN
CPE435/Lab10/lab10CaptureUAH.pcap
Normal file
BIN
CPE435/Lab10/mysql-ssl-larger.pcapng
Normal file
BIN
CPE435/Lab10/mysql_complete.pcap
Normal file
7
CPE435/Lab10/openlogo-25.html
Normal file
@ -0,0 +1,7 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
|
||||
<html><head>
|
||||
<title>404 Not Found</title>
|
||||
</head><body>
|
||||
<h1>Not Found</h1>
|
||||
<p>The requested URL /icons/debian/openlogo-25.jpg was not found on this server.</p>
|
||||
</body></html>
|
6
CPE435/Lab10/rsasnakeoil2.README
Normal file
@ -0,0 +1,6 @@
|
||||
(05/31/07: Updated to use proper delimiter and specify protocol)
|
||||
|
||||
In preferences for SSL set RSA Keylist to:
|
||||
UNIX/Linux: 127.0.0.1,443,http,/path/to/snakeoil2.key
|
||||
Windows: 127.0.0.1,443,http,c:\path\to\snakeoil2.key
|
||||
|
BIN
CPE435/Lab10/rsasnakeoil2.cap
Normal file
19
CPE435/Lab10/rsasnakeoil2.key
Normal file
@ -0,0 +1,19 @@
|
||||
-----BEGIN RSA PRIVATE KEY-----
|
||||
MIICWwIBAAKBgQCkblMUCt4s42BVmvJCpq9HEi8Xzvq63E5jVjS5unNLeEQ9xmxp
|
||||
pCWzYQKdCQQ/cj3YJ9OwWkV3tzbkJiPMEriu3qe2OoI8fCRZCviWQ4ujKTY/kX9d
|
||||
xyOUKX8Kzgq9jZsvGReq1Y7sZqI36z9XUzzyqrt5GUuQfqejmf6ETInwPQIDAQAB
|
||||
AoGAedqEWKsBIPTTtDziYYBTDnEsUxGA/685rCX7ZtQEkx4qPDlqqBMMGVW/8Q34
|
||||
hugrap+BIgSTzHcLB6I4DwiksUpR08x0hf0oxqqjMo0KykhZDfUUfxR85JHUrFZM
|
||||
GznurVhfSBXX4Il9Tgc/RPzD32FZ6gaz9sFumJh0LKKadeECQQDWOfP6+nIAvmyH
|
||||
aRINErBSlK+xv2mZ4jEKvROIQmrpyNyoOStYLG/DRPlEzAIA6oQnowGgS6gwaibg
|
||||
g7yVTgBpAkEAxH6dcwhIDRTILvtUdKSWB6vdhtXFGdebaU4cuUOW2kWwPpyIj4XN
|
||||
D+rezwfptmeOr34DCA/QKCI/BWkbFDG2tQJAVAH971nvAuOp46AMeBvwETJFg8qw
|
||||
Oqw81x02X6TMEEm4Xi+tE7K5UTXnGld2Ia3VjUWbCaUhm3rFLB39Af/IoQJAUn/G
|
||||
o5GKjtN26SLk5sRjqXzjWcVPJ/Z6bdA6Bx71q1cvFFqsi3XmDxTRz6LG4arBIbWK
|
||||
mEvrXa5jP2ZN1EC7MQJAYTfwPZ8/4x/USmA4vx9FKdADdDoZnA9ZSwezWaqa44My
|
||||
bJ0SY/WmNU+Z4ldVIkcevwwwcxqLF399hjrXWhzlBQ==
|
||||
-----END RSA PRIVATE KEY-----
|
||||
|
||||
|
||||
|
||||
|
BIN
CPE435/Lab10/snakeoil2_070531.tar
Normal file
BIN
CPE435/Lab10/snakeoil2_070531.tgz
Normal file
BIN
CPE435/Lab10/telnet-cooked.pcap
Normal file
BIN
CPE435/Lab10/telnet-raw.pcap
Normal file
BIN
CPE435/Lab10/why_edu.png
Normal file
After Width: | Height: | Size: 144 KiB |
BIN
CPE435/Lab10/why_sme.png
Normal file
After Width: | Height: | Size: 79 KiB |
BIN
CPE435/Lab11/Lab11_Part 2.pdf
Normal file
BIN
CPE435/Lab11/Lab11_Part1.pdf
Normal file
BIN
CPE435/Lab12/Lab12.pdf
Normal file
14
CPE435/Lab12/test.c
Normal file
@ -0,0 +1,14 @@
|
||||
volatile unsigned int * const UART0DR = (unsigned int *)0x101f1000;
|
||||
// UART0 is the terminal running at 0x101f1000
|
||||
// UART0DR is the register used to transmit and receive bytes
|
||||
|
||||
void print_uart0(const char *s) {
|
||||
while(*s != '\0') { /* Loop until end of string */
|
||||
*UART0DR = (unsigned int)(*s); /* Transmit char */
|
||||
s++; /* Next char */
|
||||
}
|
||||
}
|
||||
|
||||
void c_entry() {
|
||||
print_uart0("Hello world!\n");
|
||||
}
|
BIN
CPE435/Lab13/Lab13.pdf
Normal file
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 |
BIN
CPE435/Lab3/Lab03.pdf
Normal file
BIN
CPE435/Lab3/Study_Lab03.pdf
Normal file
BIN
CPE435/Lab3/Study_Lab03.pptx
Normal file
26
CPE435/Lab3/a.txt
Normal file
@ -0,0 +1,26 @@
|
||||
total 1.2M
|
||||
drwx------ 2 anw0044 student 4.0K Feb 8 20:59 .
|
||||
drwx------ 7 anw0044 student 4.0K Feb 1 16:17 ..
|
||||
-rw------- 1 anw0044 student 0 Feb 8 21:00 a.txt
|
||||
-rw------- 1 anw0044 student 189 Feb 8 20:17 a.txt
|
||||
|
||||
-rwx------ 1 anw0044 student 8.3K Feb 6 21:55 demo1
|
||||
-rw------- 1 anw0044 student 1.6K Feb 7 19:31 demo1.c
|
||||
-rwx------ 1 anw0044 student 9.8K Feb 8 09:12 demo2
|
||||
-rw------- 1 anw0044 student 1.3K Feb 8 09:12 demo2.c
|
||||
-rwx------ 1 anw0044 student 8.6K Feb 6 21:56 demo3
|
||||
-rw------- 1 anw0044 student 1.7K Feb 7 19:31 demo3.c
|
||||
-rwx------ 1 anw0044 student 8.4K Feb 7 01:09 demo4
|
||||
-rw------- 1 anw0044 student 1.7K Feb 8 01:15 demo4.c
|
||||
-rwx------ 1 anw0044 student 8.5K Feb 6 21:56 demo5
|
||||
-rw------- 1 anw0044 student 1.3K Feb 7 19:31 demo5.c
|
||||
-rwx------ 1 anw0044 student 8.4K Feb 8 02:17 example2
|
||||
-rw------- 1 anw0044 student 271 Jan 27 16:19 example2.c
|
||||
-rwx------ 1 anw0044 student 8.5K Feb 8 02:17 example3
|
||||
-rw------- 1 anw0044 student 536 Jan 27 16:19 example3.c
|
||||
-rw------- 1 anw0044 student 121K Jan 27 16:19 Lab03.pdf
|
||||
-rwx------ 1 anw0044 student 17K Feb 8 20:59 shell
|
||||
-rw------- 1 anw0044 student 7.4K Feb 8 20:58 shell.c
|
||||
-rw------- 1 anw0044 student 417K Jan 27 16:19 Study_Lab03.pdf
|
||||
-rw------- 1 anw0044 student 482K Jan 27 16:19 Study_Lab03.pptx
|
||||
-rw------- 1 anw0044 student 94 Feb 8 09:12 test.txt
|
BIN
CPE435/Lab3/demo1
Executable file
44
CPE435/Lab3/demo1.c
Normal file
@ -0,0 +1,44 @@
|
||||
/*
|
||||
Written By: Prawar Poudel
|
||||
This program is intended to showcase the use of strtok() function
|
||||
Please study about the strtok function first and compare the three outputs that you will receive here
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
int main(int argc,char* argv[])
|
||||
{
|
||||
printf("Demo Number 1\n");
|
||||
printf("================\n");
|
||||
char myString[100] = "i,want to break,this string using, both,comma and space";
|
||||
//following is the temporary string that I want to keep my char[] read from breaking the above char[] myString
|
||||
//following breaks based on space character
|
||||
char *temp;
|
||||
temp = strtok(myString," "); //include <space> inside ""
|
||||
while(temp!=NULL)
|
||||
{
|
||||
printf("%s\n",temp);
|
||||
temp = strtok(NULL," "); //include <space> inside ""
|
||||
}
|
||||
//following breaks based on comma character
|
||||
printf("\n\nDemo Number 2\n");
|
||||
printf("================\n");
|
||||
strcpy(myString,"i,want to break,this string using, both,comma and space");
|
||||
temp = strtok(myString,","); //include , inside ""
|
||||
while(temp!=NULL)
|
||||
{
|
||||
printf("%s\n",temp);
|
||||
temp = strtok(NULL,","); //include , inside ""
|
||||
}
|
||||
//following breaks based on space or comma character
|
||||
printf("\n\nDemo Number 3\n");
|
||||
printf("================\n");
|
||||
strcpy(myString,"i,want to break,this string using, both,comma and space");
|
||||
temp = strtok(myString,", "); //include both space and , inside "" while(temp!=NULL)
|
||||
while(temp!=NULL)
|
||||
{
|
||||
printf("%s\n",temp);
|
||||
temp = strtok(NULL,", "); //include both space and , inside ""
|
||||
}
|
||||
return 0;
|
||||
}
|
BIN
CPE435/Lab3/demo2
Executable file
33
CPE435/Lab3/demo2.c
Normal file
@ -0,0 +1,33 @@
|
||||
/*
|
||||
Written By: Prawar Poudel
|
||||
This program is supposed to demonstrate the execution of dup2() function
|
||||
Please read the manual page first before jumping to run this program
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
int main()
|
||||
{
|
||||
printf("You would expect this to go to your stdout, and it does\n");
|
||||
//we will create a file using open function
|
||||
char myFileName[] = "test.txt";
|
||||
//lets open the file by the name of test.txt
|
||||
int myDescriptor = open(myFileName,O_CREAT|O_RDWR|O_TRUNC,0644);
|
||||
int id;
|
||||
//creating a child that redirects the stdout to test.txt
|
||||
// you can use similar functionality for '>' operator
|
||||
if((id=fork())==0)
|
||||
{
|
||||
//lets call dup2 so that out stdout (second argument) is now copied to (points to) test.txt (first argument)
|
||||
// what this essentially means is that anything that you send to stdout will be sent to myDescriptor
|
||||
dup2(myDescriptor,1); //1 is stdout, 0 is stdin and 2 is stderr
|
||||
printf("You would expect this to go to your stdout, but since we called dup2, this will go to test.txt");
|
||||
close(myDescriptor);
|
||||
exit(0);
|
||||
}else
|
||||
wait(0);
|
||||
printf("This is also printed to the console\n");
|
||||
return 0;
|
||||
}
|
BIN
CPE435/Lab3/demo3
Executable file
49
CPE435/Lab3/demo3.c
Normal file
@ -0,0 +1,49 @@
|
||||
/*
|
||||
Written By: Prawar Poudel
|
||||
This program demonstrates the use of pipe() function in C
|
||||
Please man pipe and have understanding before going through this code
|
||||
Pipe passes information from one process to another, similar to water-pipes there is a read-end and a write-end of pipe
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/wait.h>
|
||||
int main()
|
||||
{
|
||||
int myPipingDescriptors[2];
|
||||
if(pipe(myPipingDescriptors)==-1)
|
||||
{
|
||||
printf("Error in calling the piping function\n");
|
||||
exit(0);
|
||||
}
|
||||
//at this point two pipe ends are created
|
||||
// one is the read end and other is write end
|
||||
// [0] will be the read end, [1] will be the write end
|
||||
//now lets fork two process where one will make use of the read end and other will make
|
||||
// use of write end
|
||||
// they can communicate this way through the pipe
|
||||
int id;
|
||||
if((id=fork())==0)
|
||||
{
|
||||
dup2(myPipingDescriptors[1],1); //second argument 1 is stdout
|
||||
close(myPipingDescriptors[0]); //read end is unused to lets close it
|
||||
//this following statement will not be printed since we have copied the stdout to write end of pipe
|
||||
printf("I am child, and sending this message.\n");
|
||||
exit(0);
|
||||
}else if (id>0)
|
||||
{
|
||||
wait(0);
|
||||
char myRead[100];
|
||||
//basically what's written to the write-end of pipe stays there until we read the read-end of pipe
|
||||
read(myPipingDescriptors[0],myRead,37);
|
||||
printf("I am parent. I read following statement\n\t%s\n",myRead); close(myPipingDescriptors[1]);
|
||||
}else
|
||||
{
|
||||
printf("Failed to fork so terminating the process\n");
|
||||
exit(-1);
|
||||
}
|
||||
close(myPipingDescriptors[0]);
|
||||
close(myPipingDescriptors[1]);
|
||||
return 0;
|
||||
}
|
BIN
CPE435/Lab3/demo4
Executable file
46
CPE435/Lab3/demo4.c
Normal file
@ -0,0 +1,46 @@
|
||||
/*
|
||||
Written By: Prawar Poudel
|
||||
execvp runs a program that you pass as argument
|
||||
Please study about the execvp function before going to run this program
|
||||
After you understand the things, please run and watch them
|
||||
Please make sure that execvp has the right executable provided
|
||||
*/
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/wait.h>
|
||||
int main()
|
||||
{
|
||||
char myArgument[100];
|
||||
//you can change the content of myArgument using any user typed input using gets()
|
||||
strcpy(myArgument,"date");
|
||||
//execvp expects the arguments to be provided as char[][]
|
||||
//so please make sure you understand strtok before coming here
|
||||
//we will use strtok() to break the sequence of command and argument in myArgument to convert to char[][]
|
||||
char* myBrokenArgs[10]; //this will hold the values after we tokenize
|
||||
printf("Starting tokenization...\n");
|
||||
myBrokenArgs[0] = strtok(myArgument," ");
|
||||
int counter = 0;
|
||||
while(myBrokenArgs[counter]!=NULL)
|
||||
{
|
||||
counter+=1;
|
||||
myBrokenArgs[counter] = strtok(NULL," ");
|
||||
}
|
||||
myBrokenArgs[counter] = NULL;
|
||||
printf("\ttokenization complete....\n\nNow executing using execvp\n");
|
||||
printf("Following will be the output of execvp\n");
|
||||
printf("=======================================\n");
|
||||
int id;
|
||||
//I will spawn a child that will run my execvp command
|
||||
if((id=fork())==0)
|
||||
execvp(myBrokenArgs[0],myBrokenArgs);
|
||||
else if(id<0)
|
||||
printf("Failed to make child...\n");
|
||||
else
|
||||
{
|
||||
//parent shall wait until the child is killed
|
||||
wait(0);
|
||||
printf("=======================================\n"); printf("Completed execution\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
BIN
CPE435/Lab3/demo5
Executable file
57
CPE435/Lab3/demo5.c
Normal file
@ -0,0 +1,57 @@
|
||||
/*
|
||||
program runs "ls | sort" command using 2 child processes
|
||||
|
||||
dup2 is used to duplicate an old file descriptor into a new one
|
||||
normal file descriptor table
|
||||
|
||||
0 [ standard input ]
|
||||
1 [ standard output ]
|
||||
2 [ standard error ]
|
||||
|
||||
pipe is used to communicate between child processes
|
||||
unused ends of the pipe should be closed if unused
|
||||
in that process
|
||||
|
||||
|
||||
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int fds[2];
|
||||
pipe(fds);
|
||||
// Child 1 will duplicate downstream into stdin
|
||||
if(fork() == 0)
|
||||
{
|
||||
dup2(fds[0], 0); // normally 0 is for stdin
|
||||
// will now read to fds[0]
|
||||
// end of pipe
|
||||
close(fds[1]); // close other end of pipe
|
||||
execlp("sort", "sort", 0);
|
||||
//execlp("wc", "wc", "-l", 0); // Note the first argument is the command
|
||||
// After it are the arguments including
|
||||
// original command
|
||||
}
|
||||
// Child2 duplicates upstream into stdout
|
||||
else if (fork() == 0)
|
||||
{
|
||||
dup2(fds[1], 1); // normally 1 is for stdout
|
||||
// will now write to fds[1]
|
||||
// end of pipe
|
||||
close(fds[0]); // close other end of pipe
|
||||
execlp("ls", "ls", 0);
|
||||
}
|
||||
// Parent
|
||||
else
|
||||
{
|
||||
close(fds[0]);
|
||||
close(fds[1]);
|
||||
wait(0);
|
||||
wait(0);
|
||||
}
|
||||
}
|
BIN
CPE435/Lab3/example2
Executable file
14
CPE435/Lab3/example2.c
Normal file
@ -0,0 +1,14 @@
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
char *cmd[] = {"who", "ls", "date"};
|
||||
int i;
|
||||
printf("0=who, 1=ls, 2=date :");
|
||||
scanf("%d", &i);
|
||||
execlp(cmd[i], cmd[i], 0);
|
||||
printf("command not found\n"); /*exec failed*/
|
||||
}
|
BIN
CPE435/Lab3/example3
Executable file
28
CPE435/Lab3/example3.c
Normal file
@ -0,0 +1,28 @@
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/wait.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
char *cmd[] = {"who", "ls", "date", "notacommand"};
|
||||
int i;
|
||||
while(1)
|
||||
{
|
||||
printf("0=who, 1=ls, 2=date, 3=notacommand :");
|
||||
scanf("%d", &i);
|
||||
if(fork() == 0)
|
||||
{
|
||||
execlp(cmd[i], cmd[i], 0); // If execlp runs correctly, control
|
||||
// is transfer to the new program
|
||||
printf("command not found\n"); // exec failed
|
||||
exit(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Parent waits for child process to complete
|
||||
wait(0);
|
||||
}
|
||||
}
|
||||
}
|
BIN
CPE435/Lab3/shell
Executable file
278
CPE435/Lab3/shell.c
Normal file
@ -0,0 +1,278 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
char* substr(const char *src, int m, int n);
|
||||
void pipeFunc(char *command1, char *command2);
|
||||
|
||||
int main()
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
printf("My Shell\n");
|
||||
printf(">> ");
|
||||
//following is the temporary string that I want to keep my char[] read from breaking the above char[] myString
|
||||
//following breaks based on space character
|
||||
char commandStr[100], commandStrFull[100];
|
||||
char *temp;
|
||||
char *temp2;
|
||||
char temp3[100];
|
||||
//strcpy(commandStr, "exit");
|
||||
fgets(commandStr, 100, stdin);
|
||||
//strcpy(commandStr, "ls -l > a.txt");
|
||||
strcpy(commandStrFull,commandStr);
|
||||
char *filename;
|
||||
int flag = 0;
|
||||
// exit program if command is exit
|
||||
if(strcmp(commandStr, "exit\n")==0)
|
||||
{
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
int redir=0;
|
||||
int redirIndex;
|
||||
int pipeNum=0;
|
||||
int single=0;
|
||||
char* redirCheckTwo=NULL;
|
||||
char* command2;
|
||||
char* command1;
|
||||
char* pipeCheck = strchr(commandStr, '|');
|
||||
char* redirCheck = strchr(commandStr, '>');
|
||||
if(pipeCheck!=NULL)
|
||||
{
|
||||
pipeNum=1;
|
||||
redirCheckTwo = strchr(commandStr, '>');
|
||||
command1 = strrchr(commandStr, '|');
|
||||
redirIndex = strcspn(commandStr, ">");
|
||||
}
|
||||
else single=1;
|
||||
if (redirCheckTwo!=NULL)
|
||||
{
|
||||
command1 = strrchr(commandStr, '|');
|
||||
redir=1;
|
||||
}
|
||||
if(redirCheck!=NULL) redir=1;
|
||||
//commandStr[strcspn(commandStr, "\n")] = 0;
|
||||
// pipe or redirect
|
||||
if (pipeNum==1 || redir==1)
|
||||
{
|
||||
temp = strtok(commandStr," "); //include , inside ""
|
||||
while(temp!=NULL)
|
||||
{
|
||||
//printf("%s\n",temp);
|
||||
if (strcmp(temp,">")==0 && redir==1)
|
||||
{
|
||||
filename = strtok(NULL, " ");
|
||||
//printf("Filename: %s\n", filename);
|
||||
}
|
||||
if (strcmp(temp,"|")==0 && pipeCheck!=NULL)
|
||||
{
|
||||
command2 = strtok(NULL," >");
|
||||
//puts(strchr(temp, '|'));
|
||||
flag=1;
|
||||
}
|
||||
|
||||
if (pipeNum==1 || redir==1)
|
||||
{
|
||||
temp = strtok(NULL," "); //include , inside ""
|
||||
}
|
||||
else temp2 = strtok(NULL," "); //include , inside ""
|
||||
}
|
||||
}
|
||||
// single command is run
|
||||
if(single==1 && pipeNum==0 && redir==0){
|
||||
commandStr[strcspn(commandStr, "\n")] = 0;
|
||||
//strcpy(temp,commandStr);
|
||||
|
||||
//execvp expects the arguments to be provided as char[][]
|
||||
//so please make sure you understand strtok before coming here
|
||||
//we will use strtok() to break the sequence of command and argument in myArgument to convert to char[][]
|
||||
char* myBrokenArgs[10]; //this will hold the values after we tokenize
|
||||
//printf("Starting tokenization...\n");
|
||||
myBrokenArgs[0] = strtok(commandStr," ");
|
||||
int counter = 0;
|
||||
while(myBrokenArgs[counter]!=NULL)
|
||||
{
|
||||
counter+=1;
|
||||
myBrokenArgs[counter] = strtok(NULL," ");
|
||||
}
|
||||
myBrokenArgs[counter] = NULL;
|
||||
//printf("\ttokenization complete....\n\nNow executing using execvp\n");
|
||||
//printf("Following will be the output of execvp\n");
|
||||
//printf("=======================================\n");
|
||||
if ((strcmp("ls",myBrokenArgs[0]) ==0) && single==1)
|
||||
{
|
||||
int id;
|
||||
//I will spawn a child that will run my execvp command
|
||||
if((id=fork())==0)
|
||||
execvp(myBrokenArgs[0],myBrokenArgs);
|
||||
else if(id<0)
|
||||
printf("Failed to make child...\n");
|
||||
else
|
||||
{
|
||||
//parent shall wait until the child is killed
|
||||
wait(0);
|
||||
printf("=======================================\n");
|
||||
printf("Completed execution\n");
|
||||
}
|
||||
}
|
||||
else if (strcmp("date",myBrokenArgs[0]) ==0)
|
||||
{
|
||||
int id;
|
||||
//I will spawn a child that will run my execvp command
|
||||
if((id=fork())==0)
|
||||
execvp(myBrokenArgs[0],myBrokenArgs);
|
||||
else if(id<0)
|
||||
printf("Failed to make child...\n");
|
||||
else
|
||||
{
|
||||
//parent shall wait until the child is killed
|
||||
wait(0);
|
||||
printf("=======================================\n");
|
||||
printf("Completed execution\n");
|
||||
}
|
||||
}
|
||||
else if ((strcmp("ps",myBrokenArgs[0]) ==0))
|
||||
{
|
||||
int id;
|
||||
//I will spawn a child that will run my execvp command
|
||||
if((id=fork())==0)
|
||||
execvp(myBrokenArgs[0],myBrokenArgs);
|
||||
else if(id<0)
|
||||
printf("Failed to make child...\n");
|
||||
else
|
||||
{
|
||||
//parent shall wait until the child is killed
|
||||
wait(0);
|
||||
printf("=======================================\n");
|
||||
printf("Completed execution\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
// pipe is used
|
||||
if (pipeNum==1 && redirCheck==NULL && single==0)
|
||||
{
|
||||
strcpy(commandStrFull, "ls -lah | wc -l");
|
||||
int size = strcspn(commandStrFull, "|");
|
||||
int end = strcspn(commandStrFull, "\0");
|
||||
int beg = strcspn(commandStrFull, "|");
|
||||
char* command2 = substr(commandStrFull, beg+2, end);
|
||||
|
||||
end = strcspn(commandStrFull, "|");
|
||||
char* command1 = substr(commandStrFull, 0, end-1);
|
||||
|
||||
char* command1Args[10]; //this will hold the values after we tokenize
|
||||
//printf("Starting tokenization...\n");
|
||||
command1Args[0] = strtok(command1," ");
|
||||
int counter = 0;
|
||||
while(command1Args[counter]!=NULL)
|
||||
{
|
||||
counter+=1;
|
||||
command1Args[counter] = strtok(NULL,"\0");
|
||||
}
|
||||
command1Args[counter] = NULL;
|
||||
|
||||
char* command2Args[10]; //this will hold the values after we tokenize
|
||||
//printf("Starting tokenization...\n");
|
||||
command2Args[0] = strtok(command2," ");
|
||||
counter = 0;
|
||||
while(command2Args[counter]!=NULL)
|
||||
{
|
||||
counter+=1;
|
||||
command2Args[counter] = strtok(NULL," ");
|
||||
}
|
||||
command2Args[counter] = NULL;
|
||||
|
||||
int fds[2];
|
||||
pipe(fds);
|
||||
// Child 1 will duplicate downstream into stdin
|
||||
if(fork() == 0)
|
||||
{
|
||||
dup2(fds[0], 0); // normally 0 is for stdin
|
||||
// will now read to fds[0]
|
||||
// end of pipe
|
||||
close(fds[1]); // close other end of pipe
|
||||
execlp(command2Args[0] ,command2Args[0], command2Args, 0);
|
||||
//execlp("wc", "wc", "-l", 0); // Note the first argument is the command
|
||||
// After it are the arguments including
|
||||
// original command
|
||||
}
|
||||
// Child2 duplicates upstream into stdout
|
||||
else if (fork() == 0)
|
||||
{
|
||||
dup2(fds[1], 1); // normally 1 is for stdout
|
||||
// will now write to fds[1]
|
||||
// end of pipe
|
||||
close(fds[0]); // close other end of pipe
|
||||
execlp(command1Args[0],command1Args[0], command1Args, 0);
|
||||
}
|
||||
// Parent
|
||||
else
|
||||
{
|
||||
close(fds[0]);
|
||||
close(fds[1]);
|
||||
wait(0);
|
||||
wait(0);
|
||||
}
|
||||
}
|
||||
if (pipeNum==0 && redir==1)
|
||||
{
|
||||
int end = strcspn(commandStrFull, ">");
|
||||
int beg = strcspn(commandStrFull, "|");
|
||||
char* command1 = substr(commandStrFull, 0, end-1);
|
||||
char* myBrokenArgs[10]; //this will hold the values after we tokenize
|
||||
printf("Starting tokenization...\n");
|
||||
myBrokenArgs[0] = strtok(command1," ");
|
||||
int counter = 0;
|
||||
while(myBrokenArgs[counter]!=NULL)
|
||||
{
|
||||
counter+=1;
|
||||
myBrokenArgs[counter] = strtok(NULL," ");
|
||||
}
|
||||
myBrokenArgs[counter] = NULL;
|
||||
|
||||
int myDescriptor = open(filename,O_CREAT|O_RDWR|O_TRUNC,0644);
|
||||
int id;
|
||||
//creating a child that redirects the stdout to test.txt
|
||||
// you can use similar functionality for '>' operator
|
||||
if((id=fork())==0)
|
||||
{
|
||||
//lets call dup2 so that out stdout (second argument) is now copied to (points to) test.txt (first argument)
|
||||
// what this essentially means is that anything that you send to stdout will be sent to myDescriptor
|
||||
dup2(myDescriptor,1); //1 is stdout, 0 is stdin and 2 is stderr
|
||||
execvp(myBrokenArgs[0],myBrokenArgs);
|
||||
close(myDescriptor);
|
||||
exit(0);
|
||||
}else
|
||||
wait(0);
|
||||
printf("Completed execution.\n");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
void pipeFunc(char *command1, char *command2)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
char* substr(const char *src, int m, int n)
|
||||
{
|
||||
// get the length of the destination string
|
||||
int len = n - m;
|
||||
|
||||
// allocate (len + 1) chars for destination (+1 for extra null character)
|
||||
char *dest = (char*)malloc(sizeof(char) * (len + 1));
|
||||
|
||||
// start with m'th char and copy `len` chars into the destination
|
||||
strncpy(dest, (src + m), len);
|
||||
|
||||
// return the destination string
|
||||
return dest;
|
||||
}
|
1
CPE435/Lab3/test.txt
Normal file
@ -0,0 +1 @@
|
||||
You would expect this to go to your stdout, but since we called dup2, this will go to test.txt
|
BIN
CPE435/Lab4/IPC_SharedMemory.pdf
Normal file
BIN
CPE435/Lab4/IPC_SharedMemory.pptx
Normal file
BIN
CPE435/Lab4/Lab04.pdf
Normal file
BIN
CPE435/Lab4/Process_A
Executable file
73
CPE435/Lab4/Process_A.c
Normal file
@ -0,0 +1,73 @@
|
||||
/***********************************************************************************
|
||||
Code Inherited from Anon
|
||||
Massively Modified by Prawar Poudel, 3 Feb 2020 (pp0030@uah.edu)
|
||||
Modified by Noah Eid, 30 Jan 2021 (nae0005@uah.edu)
|
||||
************************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/ipc.h>
|
||||
#include <sys/shm.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "line.h"
|
||||
|
||||
/*
|
||||
Process A creates a shared memory segment
|
||||
Process A attaches a pointer to the shared memory segment created
|
||||
.. then it writes an integer value and a character value to it
|
||||
Process A will then print that character every 4 seconds to terminal
|
||||
.. a number of times
|
||||
|
||||
Process B will can also access the shared memory segment and change
|
||||
.. the character being printed
|
||||
|
||||
After the printing operation is done, Process A will detach and delete the shared memory segment
|
||||
|
||||
*/
|
||||
main(void)
|
||||
{
|
||||
int i, id ;
|
||||
struct info *ctrl;
|
||||
|
||||
// create the shared memory segment
|
||||
// .. id is the file identifier of the shared memory segment
|
||||
// .. if the key is shared, any process can attach to this shared memory segment
|
||||
// .. 0666 is the file permission for the shared memory segment
|
||||
if ( (id = shmget( key,MSIZ,IPC_CREAT | 0666) ) < 0 )
|
||||
{
|
||||
//error…;
|
||||
printf("error 1\n");
|
||||
exit(1) ;
|
||||
}
|
||||
|
||||
// attach a local pointer to the shared memory segment created
|
||||
// .. exit if fails
|
||||
if ( (ctrl = (struct info *) shmat( id, 0, 0)) <= (struct info *) (0) )
|
||||
{
|
||||
//error … ;
|
||||
printf("error 2\n");
|
||||
exit(1) ;
|
||||
}
|
||||
|
||||
// put some initial data in the shared memory so that we will just print the things before ProcessB is run
|
||||
ctrl->c = 'a';
|
||||
ctrl->length = 10;
|
||||
|
||||
// print line every 4 seconds
|
||||
for (i = 0 ; i <ctrl->length ; i++ )
|
||||
{
|
||||
putchar (ctrl->c);
|
||||
putchar ('\n');
|
||||
sleep(4);
|
||||
}
|
||||
|
||||
//now detach the pointer from the shared memory
|
||||
shmdt(ctrl);
|
||||
//let us delete the shared memory
|
||||
shmctl(id,IPC_RMID,NULL);
|
||||
|
||||
//job done
|
||||
}
|
||||
|
BIN
CPE435/Lab4/Process_B
Executable file
57
CPE435/Lab4/Process_B.c
Normal file
@ -0,0 +1,57 @@
|
||||
/***********************************************************************************
|
||||
Code Inherited from Anon
|
||||
Massively Modified by Prawar Poudel, 3 Feb 2020 (pp0030@uah.edu)
|
||||
Modified by Noah Eid, 30 Jan 2021 (nae0005@uah.edu)
|
||||
************************************************************************************/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/ipc.h>
|
||||
#include <sys/shm.h>
|
||||
|
||||
#include "line.h"
|
||||
|
||||
|
||||
/*
|
||||
|
||||
Process B can change the contents of the shared memory that Process A is using
|
||||
./ProcessB <char> <int>
|
||||
|
||||
*/
|
||||
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int id ;
|
||||
struct info *ctrl;
|
||||
|
||||
if (argc < 3)
|
||||
{
|
||||
//error…;
|
||||
exit(3);
|
||||
}
|
||||
// get the id of the already created shared memory segment
|
||||
// .. function to create is same as getting id of the already created shared memory segment
|
||||
if ( (id = shmget( key,MSIZ, 0 )) < 0 )
|
||||
{
|
||||
//error … ;
|
||||
exit(1) ;
|
||||
}
|
||||
|
||||
// attach a local pointer to the shared memory
|
||||
ctrl = (struct info *) shmat( id, 0, 0);
|
||||
if ( ctrl <= (struct info *) (0) )
|
||||
{
|
||||
//error … ;
|
||||
exit(1) ;
|
||||
}
|
||||
|
||||
/* copy command line data to shared memory */
|
||||
ctrl->c = argv[1][0] ;
|
||||
ctrl->length = atoi(argv[2]);
|
||||
|
||||
|
||||
//detach the pointer from the shared memory
|
||||
// .. we do not need to delete here, can be done in either of the process
|
||||
shmdt(ctrl);
|
||||
exit(0);
|
||||
}
|
89
CPE435/Lab4/Process_C.c
Normal file
@ -0,0 +1,89 @@
|
||||
/***********************************************************************************
|
||||
Code Inherited from Anon
|
||||
Massively Modified by Prawar Poudel, 3 Feb 2020 (pp0030@uah.edu)
|
||||
************************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/ipc.h>
|
||||
#include <sys/shm.h>
|
||||
|
||||
#include "header.h"
|
||||
|
||||
/*
|
||||
Process C creates a shared memory segment
|
||||
Process C attaches a pointer to the shared memory segment created
|
||||
.. then it writes an integer value and a character value to it
|
||||
.. it prints the character value for the integer number of times
|
||||
.. then it waits on Process D to update the value of integer and the character
|
||||
.. .. this will be indicated by the flag value being set to 1 by Process D
|
||||
.. after the same printing operation is done, Process C will detach and delete the shared memory segment
|
||||
|
||||
*/
|
||||
main(void)
|
||||
{
|
||||
int i, id ;
|
||||
struct info *ctrl;
|
||||
|
||||
// create the shared memory segment
|
||||
// .. id is the file identifier of the shared memory segment
|
||||
// .. if the key is shared, any process can attach to this shared memory segment
|
||||
// .. 0666 is the file permission for the shared memory segment
|
||||
if ( (id = shmget( key,MSIZ,IPC_CREAT | 0666) ) < 0 )
|
||||
{
|
||||
//error…;
|
||||
printf("error 1\n");
|
||||
exit(1) ;
|
||||
}
|
||||
|
||||
// attach a local pointer to the shared memory segment created
|
||||
// .. exit if fails
|
||||
if ( (ctrl = (struct info *) shmat( id, 0, 0)) <= (struct info *) (0) ){
|
||||
//error … ;
|
||||
printf("error 2\n");
|
||||
exit(1) ; }
|
||||
|
||||
// put some initial data in the shared memory so that we will just print the things before Process D is run
|
||||
ctrl->c = 'a';
|
||||
ctrl->length = 10;
|
||||
ctrl->flag = 0;
|
||||
|
||||
// print the character ctrl->c for ctrl->length times in a single line
|
||||
for (i = 0 ; i <ctrl->length ; i++ )
|
||||
{
|
||||
putchar (ctrl->c);
|
||||
putchar (' ');
|
||||
}
|
||||
putchar('\n');
|
||||
|
||||
// flushing stdout just in case
|
||||
fflush(stdout);
|
||||
|
||||
// signal Process D that Process C is ready for next round, which will be printed below
|
||||
ctrl->flag = 2;
|
||||
|
||||
// wait for the flag to be set in processB to 1
|
||||
// .. flag is 0 right now
|
||||
// .. once processB starts to run, it will set the flag to 1
|
||||
while(ctrl->flag!=1);
|
||||
|
||||
|
||||
// print the character ctrl->c for ctrl->length times in a single line
|
||||
for (i = 0 ; i <ctrl->length ; i++ )
|
||||
{
|
||||
putchar (ctrl->c);
|
||||
putchar (' ');
|
||||
}
|
||||
putchar ('\n');
|
||||
// flushing stdout just in case
|
||||
fflush(stdout);
|
||||
|
||||
//now detach the pointer from the shared memory
|
||||
shmdt(ctrl);
|
||||
//let us delete the shared memory
|
||||
shmctl(id,IPC_RMID,NULL);
|
||||
|
||||
//job done
|
||||
}
|
||||
|
51
CPE435/Lab4/Process_D.c
Normal file
@ -0,0 +1,51 @@
|
||||
/***********************************************************************************
|
||||
Code Inherited from Anon
|
||||
Massively Modified by Prawar Poudel, 3 Feb 2020 (pp0030@uah.edu)
|
||||
************************************************************************************/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/ipc.h>
|
||||
#include <sys/shm.h>
|
||||
|
||||
#include "header.h"
|
||||
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int id ;
|
||||
struct info *ctrl;
|
||||
|
||||
if (argc < 3)
|
||||
{
|
||||
//error…;
|
||||
exit(3);
|
||||
}
|
||||
// get the id of the already created shared memory segment
|
||||
// .. function to create is same as getting id of the already created shared memory segment
|
||||
// .. notice the difference between the flags used in Process C and Process D
|
||||
if ( (id = shmget( key,MSIZ, 0 )) < 0 )
|
||||
{
|
||||
//error … ;
|
||||
exit(1) ;
|
||||
}
|
||||
|
||||
// attach a local pointer to the shared memory
|
||||
ctrl = (struct info *) shmat( id, 0, 0);
|
||||
if ( ctrl <= (struct info *) (0) )
|
||||
{
|
||||
//error … ;
|
||||
exit(1) ;
|
||||
}
|
||||
// wait until Process C is ready to receive from process D
|
||||
while(ctrl->flag==0);
|
||||
|
||||
/* copy command line data to shared memory */
|
||||
ctrl->c = argv[1][0] ;
|
||||
ctrl->length = atoi(argv[2]);
|
||||
ctrl->flag = 1;
|
||||
|
||||
//detach the pointer from the shared memory
|
||||
// .. we do not need to delete here, can be done in either of the process
|
||||
shmdt(ctrl);
|
||||
exit(0);
|
||||
}
|
7
CPE435/Lab4/header.h
Normal file
@ -0,0 +1,7 @@
|
||||
struct info {
|
||||
char c;
|
||||
int length;
|
||||
char flag;
|
||||
};
|
||||
key_t key = 1243 ;
|
||||
#define MSIZ sizeof(struct info)
|