diff --git a/OneDrive/Desktop/Artificial Intelligence/Practical/Practical 3/._exec.c b/OneDrive/Desktop/Artificial Intelligence/Practical/Practical 3/._exec.c new file mode 100644 index 0000000000000000000000000000000000000000..fdd858c0bd277141d1b18ba238415ef63cc5267a Binary files /dev/null and b/OneDrive/Desktop/Artificial Intelligence/Practical/Practical 3/._exec.c differ diff --git a/OneDrive/Desktop/Artificial Intelligence/Practical/Practical 3/._fork.c b/OneDrive/Desktop/Artificial Intelligence/Practical/Practical 3/._fork.c new file mode 100644 index 0000000000000000000000000000000000000000..fdd858c0bd277141d1b18ba238415ef63cc5267a Binary files /dev/null and b/OneDrive/Desktop/Artificial Intelligence/Practical/Practical 3/._fork.c differ diff --git a/OneDrive/Desktop/Artificial Intelligence/Practical/Practical 3/._pipe.c b/OneDrive/Desktop/Artificial Intelligence/Practical/Practical 3/._pipe.c new file mode 100644 index 0000000000000000000000000000000000000000..fdd858c0bd277141d1b18ba238415ef63cc5267a Binary files /dev/null and b/OneDrive/Desktop/Artificial Intelligence/Practical/Practical 3/._pipe.c differ diff --git a/OneDrive/Desktop/Artificial Intelligence/Practical/Practical 3/._thread1.c b/OneDrive/Desktop/Artificial Intelligence/Practical/Practical 3/._thread1.c new file mode 100644 index 0000000000000000000000000000000000000000..fdd858c0bd277141d1b18ba238415ef63cc5267a Binary files /dev/null and b/OneDrive/Desktop/Artificial Intelligence/Practical/Practical 3/._thread1.c differ diff --git a/OneDrive/Desktop/Artificial Intelligence/Practical/Practical 3/._thread2.c b/OneDrive/Desktop/Artificial Intelligence/Practical/Practical 3/._thread2.c new file mode 100644 index 0000000000000000000000000000000000000000..fdd858c0bd277141d1b18ba238415ef63cc5267a Binary files /dev/null and b/OneDrive/Desktop/Artificial Intelligence/Practical/Practical 3/._thread2.c differ diff --git a/OneDrive/Desktop/Artificial Intelligence/Practical/Practical 3/exec.c b/OneDrive/Desktop/Artificial Intelligence/Practical/Practical 3/exec.c new file mode 100644 index 0000000000000000000000000000000000000000..8f4831952d689e0a3262f4c70e52f4582ad1d531 --- /dev/null +++ b/OneDrive/Desktop/Artificial Intelligence/Practical/Practical 3/exec.c @@ -0,0 +1,5 @@ +#include <unistd.h> + +int main(int argc, char** argv) { + return execv("/usr/bin/ls", argv); +} diff --git a/OneDrive/Desktop/Artificial Intelligence/Practical/Practical 3/fork.c b/OneDrive/Desktop/Artificial Intelligence/Practical/Practical 3/fork.c new file mode 100644 index 0000000000000000000000000000000000000000..62dff97d6fee823037d96b929f4fa256f50571eb --- /dev/null +++ b/OneDrive/Desktop/Artificial Intelligence/Practical/Practical 3/fork.c @@ -0,0 +1,26 @@ +#include <stdio.h> +#include <sys/types.h> +#include <unistd.h> + +int main(int argc, char** argv) { + pid_t root = getpid(); + // when forking, program does not start again since memory/register values are exactly the same + // i.e. instruction pointer is at the same line too. So fork() wont execute again. + pid_t pid = fork(); + printf("from %d forking into %d\n", root, pid); + sleep(20); + + // watch 2 different PIDs spawn 2 more child processes. + pid_t mypid = getpid(); + pid = fork(); + printf("from %d forking into %d\n", mypid, pid); + sleep(20); + + if (getpid() == root) { + sleep(20); + printf("root exiting\n"); + } else { + printf("Child -- PID %d exiting\n", getpid()); + } + return 0; +} diff --git a/OneDrive/Desktop/Artificial Intelligence/Practical/Practical 3/pipe.c b/OneDrive/Desktop/Artificial Intelligence/Practical/Practical 3/pipe.c new file mode 100644 index 0000000000000000000000000000000000000000..a2c58c53df7a8156ecb8d3dc6d6c13b1c38e4fa8 --- /dev/null +++ b/OneDrive/Desktop/Artificial Intelligence/Practical/Practical 3/pipe.c @@ -0,0 +1,44 @@ +/***************************************************************************** + Excerpt from "Linux Programmer's Guide - Chapter 6" + (C)opyright 1994-1995, Scott Burkett + ***************************************************************************** + MODULE: pipe.c + *****************************************************************************/ + +#include <stdio.h> +#include <sys/types.h> +#include <unistd.h> +#include <stdlib.h> +#include <string.h> + +int main(void) { + int fd[2], nbytes; + pid_t childpid; + char string[] = "Hello, world!\n"; + char readbuffer[80]; + + pipe(fd); + + if ((childpid = fork()) == -1) { + perror("fork"); + exit(1); + } + + if (childpid == 0) { + /* Child process closes up input side of pipe */ + close(fd[0]); + + /* Send "string" through the output side of pipe */ + write(fd[1], string, (strlen(string) + 1)); + exit(0); + } else { + /* Parent process closes up output side of pipe */ + close(fd[1]); + + /* Read in a string from the pipe */ + nbytes = read(fd[0], readbuffer, sizeof(readbuffer)); + printf("Received string: %s", readbuffer); + } + + return (0); +} diff --git a/OneDrive/Desktop/Artificial Intelligence/Practical/Practical 3/thread1.c b/OneDrive/Desktop/Artificial Intelligence/Practical/Practical 3/thread1.c new file mode 100644 index 0000000000000000000000000000000000000000..8bdba749a695b96de63be5aacd9aa2cc2e99f26d --- /dev/null +++ b/OneDrive/Desktop/Artificial Intelligence/Practical/Practical 3/thread1.c @@ -0,0 +1,27 @@ +/************************************* + Demo for pthread commands + compile: gcc threadX.c -o threadX -lpthread +***************************************/ + +#include <pthread.h> +#include <stdio.h> + +void* say_hello(void* param); /* the work_function */ + +int main(int args, char** argv) { + pthread_t tid; /* thread identifier */ + + /* create the thread */ + pthread_create(&tid, NULL, say_hello, NULL); + + /* wait for thread to exit */ + pthread_join(tid, NULL); + + printf("Hello from first thread\n"); + return 0; +} + +void* say_hello(void* param) { + printf("Hello from second thread\n"); + return NULL; +} diff --git a/OneDrive/Desktop/Artificial Intelligence/Practical/Practical 3/thread2.c b/OneDrive/Desktop/Artificial Intelligence/Practical/Practical 3/thread2.c new file mode 100644 index 0000000000000000000000000000000000000000..35408f54b3161f7cbd13504aefea942537ee3964 --- /dev/null +++ b/OneDrive/Desktop/Artificial Intelligence/Practical/Practical 3/thread2.c @@ -0,0 +1,58 @@ +#include <pthread.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#define ITERATIONS 1000000 + +void* runner(void* param); /* thread doing the work */ + +int count = 0; +pthread_mutex_t lock; + +int main(int argc, char** argv) { + pthread_t tid1, tid2; + + if (pthread_mutex_init(&lock, NULL) != 0) { + printf("mutex init failed\n"); + exit(1); + } + + if (pthread_create(&tid1, NULL, runner, NULL)) { + printf("Error creating thread 1\n"); + exit(1); + } + if (pthread_create(&tid2, NULL, runner, NULL)) { + printf("Error creating thread 2\n"); + exit(1); + } + + /* wait for the threads to finish */ + if (pthread_join(tid1, NULL)) { + printf("Error joining thread\n"); + exit(1); + } + if (pthread_join(tid2, NULL)) { + printf("Error joining thread\n"); + exit(1); + } + + if (count != 2 * ITERATIONS) + printf("** ERROR ** count is [%d], should be %d\n", count, 2 * ITERATIONS); + else + printf("OK! count is [%d]\n", count); + + pthread_exit(NULL); + pthread_mutex_destroy(&lock); +} + +/* thread doing the work */ +void* runner(void* param) { + int i, temp; + for (i = 0; i < ITERATIONS; i++) { + temp = count; /* copy the global count locally */ + temp = temp + 1; /* increment the local copy */ + count = temp; /* store the local value into the global count */ + } + return NULL; +} diff --git a/OneDrive/Desktop/Web Information Technologies/Git Project/COMP30005AwesomeProject b/OneDrive/Desktop/Web Information Technologies/Git Project/COMP30005AwesomeProject new file mode 160000 index 0000000000000000000000000000000000000000..547a7d38d3336f5a9d3e866a4d3df9c8f2079160 --- /dev/null +++ b/OneDrive/Desktop/Web Information Technologies/Git Project/COMP30005AwesomeProject @@ -0,0 +1 @@ +Subproject commit 547a7d38d3336f5a9d3e866a4d3df9c8f2079160