achilleaspappas / Operating-Systems-2

Linux - Fork - Threads

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Operating-Systems-2

The purpose of this project is to implement fork and threads. The source code files address different exercise problems. This project was part of a course I did in my 4th semester.

Prerequisites

To use the files in this repository, you will need the following:

  • GCC in order to run the files.

Getting Started

To get started with this project, follow these steps:

  1. Clone this repository to your local machine.
  2. Compile the source code file.
  3. Run the executable file generated by the compilation process.

Contents

This repository contains the following files:

  1. Source_Code_1.2.c
  2. Source_Code_1.3.c
  3. Source_Code_2.1.c
  4. Source_Code_2.2.c

Specifications

Functionality

Exercise 1 - Source_Code_1.2.c, Source_Code_1.3.c

  1. Write a C program with the appropriate fork() commands so that they are generated as a whole (along with the main program) six processes, with the following affinity structure (tree). As the main part of its work each Pi process should simply print a message to the screen stating its name (Pi), its PID and its PPID. Your program should additionally include appropriate wait-communication statements to satisfy the following constraints-requirements: (a) process P0 waits for process P2 to complete before performing the main part of its work, and (b) the process P2 before executing its main task to wait for the completion of all its children. Also process P0 before creating P2 waits to receive a 'hello from your first child' message from process P1 (and print it to the screen). Finally, the P0 process after its execution should be replaced by the ps command.

  2. Write a program in C which calculates "in parallel" the sum of the squares of the elements ) [with the help of 'p' threads of a vector A (a1^2+a2^2+…+an^2) where each thread will calculate the individual 'n/p' terms (squares) of the sum – p, n, ai should be given by the user or read from a file – also assume that 'n' is an integer multiple of ' p']. More specifically, each thread should (a) calculate locally (in a local variable – e.g. local_sum) the partial sum attributed to it and (b) update at the end with its local sum a common variable (eg total_sum) which will represent the final sum. This final sum should eventually be printed to the screen. Is there any critical part/code somewhere that requires protection? If so, write extra code to protect it this critical section using the 'locking' mechanism provided by POSIX Threads.

Exercise 2 - Source_Code_2.1.c, Source_Code_2.2.c

  1. Write a program in C and using the Pthreads library, the execution of which will recursively print the sequence:
<one><two><three><one><two><three><one><two><three>...

You should (to achieve the above) start three (3) different threads in your program (one printing , another printing and the third printing ), and then synchronize them properly with each other. For the required synchronization you should use semaphores.

  1. Let A be a two-dimensional matrix of integers (ÿxN). Write a program which will find the 'maximum' element (m) of the array A [using 'p' threads where each thread will compute the individual maximum of N/p rows of the array – the 'p', 'N' , 'A' should be supplied by the user or read from a file – also assume that 'N' is an integer multiple of 'p']. Then the program should also create in parallel (with the help of the 'p' threads that have been created from the beginning) a new matrix D (ÿxN) (which it will print at the end on the screen), in which the 'distance' will be printed of each element (Aij) of matrix A from m : Dij = m – Aij

Contributing

This is a university project so you can not contribute.

Authors

  • [University of West Attica] - Provided the exersice
  • [Achilleas Pappas] - Wrote the code

License

This project is licensed by University of West Attica as is a part of a university course. Do not redistribute.

Acknowledgments

Thank you to University of West Attica and my professors for providing the resources and knowledge to complete this project.

About

Linux - Fork - Threads


Languages

Language:C 100.0%