os:lab04
Return to Home page
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/os/lab04?do=diff&rev2%5B0%5D=1571864806&rev2%5B1%5D=1573024653&difftype=sidebyside
Differences
This shows you the differences between two versions of the page.
— | os:lab04 [2024/04/08 22:35] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | Return to [[os: | ||
+ | ---- | ||
+ | ====== Operating Systems Course: Lab04 ====== | ||
+ | ===== Laboratory number 04 ===== | ||
+ | |||
+ | |||
+ | ==== Exercise 01: Shell commands for processes and redirection ==== | ||
+ | |||
+ | === A. === | ||
+ | Write a C program that reads integer numbers and prints in standard output ('' | ||
+ | <code c> | ||
+ | fprintf (stdout, ...); | ||
+ | </ | ||
+ | the even numbers and in standard error ('' | ||
+ | <code c> | ||
+ | fprintf (stderr, ...); | ||
+ | </ | ||
+ | the odd numbers. The program must terminate after the introduction of the value '' | ||
+ | |||
+ | Make use of redirection for: | ||
+ | * read integer numbers from the file "'' | ||
+ | * write even numbers to the file "'' | ||
+ | * write the odd numbers to the file "'' | ||
+ | |||
+ | === B. === | ||
+ | Use pipe and redirection on shell command to perform the following operations: | ||
+ | * count the number of files/ | ||
+ | * after downloading the file [[https:// | ||
+ | * count the number of active processes in the system who are owned by the user "'' | ||
+ | |||
+ | |||
+ | ==== Exercise 02: Concurrent exchange-sort on files ==== | ||
+ | |||
+ | === Premise number 1. === | ||
+ | The sorting algorithms by exchange (exchange-sort or bubble-sort) sort a vector comparing adjacent elements. | ||
+ | The following is a possible implementation (with sorting of the vector v of n elements): | ||
+ | <code c> | ||
+ | for (i=0; i<n-1; i++) { | ||
+ | for (j=0; j<n-1-i; j++) { | ||
+ | if (v[j] > v[j+1]) { | ||
+ | val = v[j]; | ||
+ | v[j] = v[j+1]; | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === Premise number 2. === | ||
+ | A binary file can be write (UNIX function '' | ||
+ | |||
+ | For example, the program [[https:// | ||
+ | * it writes in the file the vector '' | ||
+ | * it reads the file starting from the last line. | ||
+ | |||
+ | === Exercise specifications === | ||
+ | Implement in a **concurrent** and **multi-process** way the bubble-sort algorithm. | ||
+ | |||
+ | The algorithm, by using the system calls '' | ||
+ | |||
+ | The program (i.e., the parent): | ||
+ | * receives on the command line the name of a file containing a predefined number '' | ||
+ | * at each iteration of the external cycle, it " | ||
+ | |||
+ | Each of its children: | ||
+ | * reads two adjacent elements from the file | ||
+ | * swaps them if it is necessary | ||
+ | * writes them in the file | ||
+ | * it synchronizes with the other children processes to generate the overall sorting effect (i.e., at each iteration in the external cycle the highest value among the remaining numbers is moved at the end of the file) | ||
+ | * terminates | ||
+ | |||
+ | === Observations === | ||
+ | - Working on file is indispensable since a parent process and a child do not share the address space (in fact, ordering a vector would actually order different vectors). | ||
+ | - The program can be generalized by considering the first integer number stored in the file as indicator of the actual number of the integer numbers saved in the file. | ||
+ | - Managing a binary file simplifies the positioning on a specific data '' | ||
+ | * number 5 | ||
+ | * ASCII coding = 1 character | ||
+ | * binary coding = '' | ||
+ | * number 12345 | ||
+ | * ASCII coding = 5 characters | ||
+ | * binary coding = '' | ||
+ | * In addition, in the first case of ASCII coding, it would also be necessary to count " | ||
+ | |||
+ | |||
+ | ==== Exercise 03: Use of signals ==== | ||
+ | Write a program that, using signals, synchronize two processes (a parent and it child) in a way that they print alternatively a specific message. | ||
+ | |||
+ | The following is an example of output: | ||
+ | <code bash> | ||
+ | Child Woke-up | ||
+ | Father Woke-up | ||
+ | Child Woke-up | ||
+ | Father Woke-up | ||
+ | Child Woke-up | ||
+ | Father Woke-up | ||
+ | Child Woke-up | ||
+ | Father Woke-up | ||
+ | ... | ||
+ | </ | ||
+ | |||
+ | Insert appropriate '' | ||
+ | |||
+ | |||
+ | ==== Exercise 04: Use of signals and communication through files ==== | ||
+ | Implement a C program that generates two children, a producer and a consumer. | ||
+ | The producer child reads from the keyboard some strings, and it transfers them to the consumer. | ||
+ | The consumer child converts the strings into uppercase characters, and it visualizes the strings in standard output. | ||
+ | |||
+ | The introduction of the string " | ||
+ | The transfer of the strings has to take place through a file. | ||
+ | |||
+ | Suggestions: | ||
+ | - Derive the solution from that of the previous exercise | ||
+ | - Communicating processes are not a parent and a child, but two " | ||
+ | |||
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/os/lab04?do=diff&rev2%5B0%5D=1571864806&rev2%5B1%5D=1573024653&difftype=sidebyside
/web/htdocs/www.skenz.it/home/data/pages/os/lab04.txt · Last modified: 2024/04/08 22:35 by 127.0.0.1