diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pipes/makefifo.sh b/install/usr/share/swarmlab.io/sec/project/courses/pipes/makefifo.sh new file mode 100755 index 0000000..93fd6d4 --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/pipes/makefifo.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +fifo=/tmp/testpipe +rm -f $fifo +mkfifo -m a=rw $fifo diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pipes/par-from-ch b/install/usr/share/swarmlab.io/sec/project/courses/pipes/par-from-ch new file mode 100755 index 0000000..c38bd38 Binary files /dev/null and b/install/usr/share/swarmlab.io/sec/project/courses/pipes/par-from-ch differ diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pipes/par-from-ch.c b/install/usr/share/swarmlab.io/sec/project/courses/pipes/par-from-ch.c new file mode 100644 index 0000000..199faed --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/pipes/par-from-ch.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include +#include +#include + + +char *phrase = "My child sent me this!!"; + +int main() +{ + int fd[2], bytesread; + char message[100]; + + pipe(fd); + + if (fork()==0) { + close(fd[0]); + write (fd[1],phrase,strlen(phrase)+1); + close(fd[1]); + } else { + close(fd[1]); + bytesread=read(fd[0],message,100); + printf("Read %d bytes:%s\n", bytesread, message); + close(fd[0]); + } +} diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pipes/rf b/install/usr/share/swarmlab.io/sec/project/courses/pipes/rf new file mode 100755 index 0000000..c3a7efc Binary files /dev/null and b/install/usr/share/swarmlab.io/sec/project/courses/pipes/rf differ diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pipes/rf.c b/install/usr/share/swarmlab.io/sec/project/courses/pipes/rf.c new file mode 100644 index 0000000..e58809a --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/pipes/rf.c @@ -0,0 +1,41 @@ +#include +#include +#include +#include +#include +#include + +// C program to implement one side of FIFO +// This side reads first, then writes +int main() +{ + int fd1; + + // FIFO file path + char * myfifo = "/tmp/myfifo"; + + // Creating the named file(FIFO) + // mkfifo(,) + mkfifo(myfifo, 0666); + + char str1[80], str2[80]; + while (1) + { + // First open in read only and read + fd1 = open(myfifo,O_RDONLY); + read(fd1, str1, 80); + + // Print the read string and close + printf("User1: %s\n", str1); + close(fd1); + + // Now open in write mode and write + // string taken from user. + fd1 = open(myfifo,O_WRONLY); + fgets(str2, 80, stdin); + write(fd1, str2, strlen(str2)+1); + close(fd1); + } + return 0; +} + diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pipes/shell-pipe.sh b/install/usr/share/swarmlab.io/sec/project/courses/pipes/shell-pipe.sh new file mode 100755 index 0000000..c79dcf6 --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/pipes/shell-pipe.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +for i in `seq 10 30`; do + echo $i + sleep 1 +done > /tmp/testpipe + diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pipes/shell-pipe1.sh b/install/usr/share/swarmlab.io/sec/project/courses/pipes/shell-pipe1.sh new file mode 100755 index 0000000..0d10fba --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/pipes/shell-pipe1.sh @@ -0,0 +1,7 @@ +#!/bin/sh + + +for i in `seq 10 30`; do + echo "allo programma: $i " + sleep 1 +done > /tmp/testpipe diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pipes/shell-pipe2.sh b/install/usr/share/swarmlab.io/sec/project/courses/pipes/shell-pipe2.sh new file mode 100755 index 0000000..9f536d6 --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/pipes/shell-pipe2.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +for i in `seq 10 30`; do + echo $i > /tmp/testpipe + sleep 1 +done + diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pipes/wf b/install/usr/share/swarmlab.io/sec/project/courses/pipes/wf new file mode 100755 index 0000000..71c04d3 Binary files /dev/null and b/install/usr/share/swarmlab.io/sec/project/courses/pipes/wf differ diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pipes/wf.c b/install/usr/share/swarmlab.io/sec/project/courses/pipes/wf.c new file mode 100644 index 0000000..ce17aac --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/pipes/wf.c @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include +#include + +// C program to implement one side of FIFO +// This side writes first, then reads + +int main() +{ + int fd; + + // FIFO file path + char * myfifo = "/tmp/myfifo"; + + // Creating the named file(FIFO) + // mkfifo(, ) + mkfifo(myfifo, 0666); + + char arr1[80], arr2[80]; + while (1) + { + // Open FIFO for write only + fd = open(myfifo, O_WRONLY); + + // Take an input arr2ing from user. + // 80 is maximum length + fgets(arr2, 80, stdin); + + // Write the input arr2ing on FIFO + // and close it + write(fd, arr2, strlen(arr2)+1); + close(fd); + + // Open FIFO for Read only + fd = open(myfifo, O_RDONLY); + + // Read from FIFO + read(fd, arr1, sizeof(arr1)); + + // Print the read message + printf("User 2: %s\n", arr1); + close(fd); + } + return 0; +} + diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pthread/CreationTermination b/install/usr/share/swarmlab.io/sec/project/courses/pthread/CreationTermination new file mode 100755 index 0000000..2856dba Binary files /dev/null and b/install/usr/share/swarmlab.io/sec/project/courses/pthread/CreationTermination differ diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pthread/CreationTermination.c b/install/usr/share/swarmlab.io/sec/project/courses/pthread/CreationTermination.c new file mode 100644 index 0000000..9e2fc47 --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/pthread/CreationTermination.c @@ -0,0 +1,57 @@ +#include +#include +#include + +void *print_message_function( void *ptr ); + +int main() +{ + pthread_t thread1, thread2; + char *message1 = "Thread 1"; + char *message2 = "Thread 2"; + int iret1, iret2; + + /* Create independent threads each of which will execute function */ + + iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1); + iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2); + + /* + Arguments: + thread - returns the thread id. (unsigned long int defined in bits/pthreadtypes.h) + attr - Set to NULL if default thread attributes are used. + void * (*start_routine) - pointer to the function to be threaded. Function has a single argument: pointer to void. + *arg - pointer to argument of function. To pass multiple arguments, send a pointer to a structure. + + The following is a list of "thread attributes" and their default values + Attribute Default Meaning of default + contentionscope PTHREAD_SCOPE_PROCESS Thread will compete for resources within the process + detachstate PTHREAD_CREATE_JOINABLE Thread is joinable by other threads + stackaddr NULL Workspace (stack) used by thread is allocated (reserved) by the operating system + stacksize NULL Workspace size used by thread is determined by the operating system + priority 0 Priority of the thread (the lower the priority value, the higher the priority...) + policy SCHED_OTHER The scheduling policy is determined by the system + inheritsched PTHREAD_EXPLICIT_SCHED scheduling policy and parameters are not inherited but explicitly defined by the attribute object + guardsize PAGESIZE size of guard area for a thread's created stack (this area will help determine if the thread has exceeded the total amount of space that was allocated for the thread) + + See https://man7.org/linux/man-pages/man3/pthread_create.3.html + */ + + /* Wait till threads are complete before main continues. Unless we */ + /* wait we run the risk of executing an exit which will terminate */ + /* the process and all threads before the threads have completed. */ + + pthread_join( thread1, NULL); + pthread_join( thread2, NULL); + + printf("Thread 1 returns: %d\n",iret1); + printf("Thread 2 returns: %d\n",iret2); + pthread_exit(NULL); +} + +void *print_message_function( void *ptr ) +{ + char *message; + message = (char *) ptr; + printf("%s \n", message); +} diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pthread/CreationTermination.c.sh b/install/usr/share/swarmlab.io/sec/project/courses/pthread/CreationTermination.c.sh new file mode 100755 index 0000000..8ef65ae --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/pthread/CreationTermination.c.sh @@ -0,0 +1 @@ +gcc -pthread -o CreationTermination CreationTermination.c diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pthread/CreationTermination1 b/install/usr/share/swarmlab.io/sec/project/courses/pthread/CreationTermination1 new file mode 100755 index 0000000..73424b4 Binary files /dev/null and b/install/usr/share/swarmlab.io/sec/project/courses/pthread/CreationTermination1 differ diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pthread/CreationTermination1.c b/install/usr/share/swarmlab.io/sec/project/courses/pthread/CreationTermination1.c new file mode 100644 index 0000000..3cedfc4 --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/pthread/CreationTermination1.c @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include + +pthread_t tid[2]; +int ret1,ret2; + +void* doSomeThing(void *arg) +{ + unsigned long i = 0; + pthread_t id = pthread_self(); + + for(i=0; i<(4294967295);i++); + + if(pthread_equal(id,tid[0])) + { + printf("\n First thread processing done\n"); + ret1 = 1; + pthread_exit(&ret1); + } + else + { + printf("\n Second thread processing done\n"); + ret2 = 2; + pthread_exit(&ret2); + } + + return NULL; +} + +int main(void) +{ + int i = 0; + int err; + int *ptr[2]; + + while(i < 2) + { + err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL); + if (err != 0) + printf("\ncan't create thread :[%s]", strerror(err)); + else + printf("\n Thread created successfully\n"); + + i++; + } + + pthread_join(tid[0], (void**)&(ptr[0])); + pthread_join(tid[1], (void**)&(ptr[1])); + + printf("\n return value from first thread is [%d]\n", *ptr[0]); + printf("\n return value from second thread is [%d]\n", *ptr[1]); + + return 0; +} diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pthread/CreationTermination1.c.sh b/install/usr/share/swarmlab.io/sec/project/courses/pthread/CreationTermination1.c.sh new file mode 100755 index 0000000..5ef5258 --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/pthread/CreationTermination1.c.sh @@ -0,0 +1 @@ +gcc -pthread -o CreationTermination1 CreationTermination1.c