Write a multithreaded program (using pthread in Linux) that calculates various s
ID: 3530271 • Letter: W
Question
Write a multithreaded program (using pthread in Linux) that calculates various statistics values for a list of numbers. This program will be passed a series of numbers on the command line and will then create three separate worker threads. One thread will determine the average of the numbers, the second will determine the maximum value, and the third will determine the minimum value. The variables representing the average, minimum, and maximum values will be stored globally. The worker threads will set these values, and the parent thread will output the values one the workers have exited. Make sure it is able to compile a program prog.c with pthread, doExplanation / Answer
The proc file system is a pseudo-file system which is used as an interface to kernel data structures. It is commonly mounted at /proc. Most of it is read-only, but some files allow kernel variables to be changed. The following outline gives a quick tour through the /proc hierarchy. /proc/[pid] There is a numerical subdirectory for each running process; the subdirectory is named by the process ID. Each such subdirectory contains the following pseudo-files and directories. /proc/[pid]/auxv (since 2.6.0-test7) This contains the contents of the ELF interpreter information passed to the process at exec time. The format is one unsigned long ID plus one unsigned long value for each entry. The last entry contains two zeros. /proc/[pid]/cgroup (since Linux 2.6.24) This file describes control groups to which the process/task belongs. For each cgroup hierarchy there is one entry containing colon-separated fields of the form: 5:cpuacct,cpu,cpuset:/daemons The colon-separated fields are, from left to right: 1. hierarchy ID number 2. set of subsystems bound to the hierarchy 3. control group in the hierarchy to which the process belongs This file is only present if the CONFIG_CGROUPS kernel configuration option is enabled. /proc/[pid]/cmdline This holds the complete command line for the process, unless the process is a zombie. In the latter case, there is nothing in this file: that is, a read on this file will return 0 characters. The command-line arguments appear in this file as a set of strings separated by null bytes ('