Browse Source

pipes

master
zeus 4 years ago
parent
commit
0fe7451cd9
  1. 5
      install/usr/share/swarmlab.io/sec/project/courses/pipes/makefifo.sh
  2. BIN
      install/usr/share/swarmlab.io/sec/project/courses/pipes/par-from-ch
  3. 28
      install/usr/share/swarmlab.io/sec/project/courses/pipes/par-from-ch.c
  4. BIN
      install/usr/share/swarmlab.io/sec/project/courses/pipes/rf
  5. 41
      install/usr/share/swarmlab.io/sec/project/courses/pipes/rf.c
  6. 7
      install/usr/share/swarmlab.io/sec/project/courses/pipes/shell-pipe.sh
  7. 7
      install/usr/share/swarmlab.io/sec/project/courses/pipes/shell-pipe1.sh
  8. 7
      install/usr/share/swarmlab.io/sec/project/courses/pipes/shell-pipe2.sh
  9. BIN
      install/usr/share/swarmlab.io/sec/project/courses/pipes/wf
  10. 49
      install/usr/share/swarmlab.io/sec/project/courses/pipes/wf.c
  11. BIN
      install/usr/share/swarmlab.io/sec/project/courses/pthread/CreationTermination
  12. 57
      install/usr/share/swarmlab.io/sec/project/courses/pthread/CreationTermination.c
  13. 1
      install/usr/share/swarmlab.io/sec/project/courses/pthread/CreationTermination.c.sh
  14. BIN
      install/usr/share/swarmlab.io/sec/project/courses/pthread/CreationTermination1
  15. 57
      install/usr/share/swarmlab.io/sec/project/courses/pthread/CreationTermination1.c
  16. 1
      install/usr/share/swarmlab.io/sec/project/courses/pthread/CreationTermination1.c.sh

5
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

BIN
install/usr/share/swarmlab.io/sec/project/courses/pipes/par-from-ch

Binary file not shown.

28
install/usr/share/swarmlab.io/sec/project/courses/pipes/par-from-ch.c

@ -0,0 +1,28 @@
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
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]);
}
}

BIN
install/usr/share/swarmlab.io/sec/project/courses/pipes/rf

Binary file not shown.

41
install/usr/share/swarmlab.io/sec/project/courses/pipes/rf.c

@ -0,0 +1,41 @@
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
// 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(<pathname>,<permission>)
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;
}

7
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

7
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

7
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

BIN
install/usr/share/swarmlab.io/sec/project/courses/pipes/wf

Binary file not shown.

49
install/usr/share/swarmlab.io/sec/project/courses/pipes/wf.c

@ -0,0 +1,49 @@
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
// 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(<pathname>, <permission>)
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;
}

BIN
install/usr/share/swarmlab.io/sec/project/courses/pthread/CreationTermination

Binary file not shown.

57
install/usr/share/swarmlab.io/sec/project/courses/pthread/CreationTermination.c

@ -0,0 +1,57 @@
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
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);
}

1
install/usr/share/swarmlab.io/sec/project/courses/pthread/CreationTermination.c.sh

@ -0,0 +1 @@
gcc -pthread -o CreationTermination CreationTermination.c

BIN
install/usr/share/swarmlab.io/sec/project/courses/pthread/CreationTermination1

Binary file not shown.

57
install/usr/share/swarmlab.io/sec/project/courses/pthread/CreationTermination1.c

@ -0,0 +1,57 @@
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
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;
}

1
install/usr/share/swarmlab.io/sec/project/courses/pthread/CreationTermination1.c.sh

@ -0,0 +1 @@
gcc -pthread -o CreationTermination1 CreationTermination1.c
Loading…
Cancel
Save