Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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, do

Explanation / 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 (''), with a further null byte after the last string. /proc/[pid]/coredump_filter (since kernel 2.6.23) See core(5). /proc/[pid]/cpuset (since kernel 2.6.12) See cpuset(7). /proc/[pid]/cwd This is a symbolic link to the current working directory of the process. To find out the current working directory of process 20, for instance, you can do this: $ cd /proc/20/cwd; /bin/pwd Note that the pwd command is often a shell built-in, and might not work properly. In bash(1), you may use pwd -P. In a multithreaded process, the contents of this symbolic link are not available if the main thread has already terminated (typically by calling pthread_exit(3)). /proc/[pid]/environ This file contains the environment for the process. The entries are separated by null bytes (''), and there may be a null byte at the end. Thus, to print out the environment of process 1, you would do: $ (cat /proc/1/environ; echo) | tr '' ' ' /proc/[pid]/exe Under Linux 2.2 and later, this file is a symbolic link containing the actual pathname of the executed command. This symbolic link can be dereferenced normally; attempting to open it will open the executable. You can even type /proc/[pid]/exe to run another copy of the same executable as is being run by process [pid]. In a multithreaded process, the contents of this symbolic link are not available if the main thread has already terminated (typically by calling pthread_exit(3)). Under Linux 2.0 and earlier /proc/[pid]/exe is a pointer to the binary which was executed, and appears as a symbolic link. A readlink(2) call on this file under Linux 2.0 returns a string in the format: [device]:inode For example, [0301]:1502 would be inode 1502 on device major 03 (IDE, MFM, etc. drives) minor 01 (first partition on the first drive). find(1) with the -inum option can be used to locate the file. /proc/[pid]/fd This is a subdirectory containing one entry for each file which the process has open, named by its file descriptor, and which is a symbolic link to the actual file. Thus, 0 is standard input, 1 standard output, 2 standard error, etc. In a multithreaded process, the contents of this directory are not available if the main thread has already terminated (typically by calling pthread_exit(3)). Programs that will take a filename as a command-line argument, but will not take input from standard input if no argument is supplied, or that write to a file named as a command-line argument, but will not send their output to standard output if no argument is supplied, can nevertheless be made to use standard input or standard out using /proc/[pid]/fd. For example, assuming that -i is the flag designating an input file and -o is the flag designating an output file: $ foobar -i /proc/self/fd/0 -o /proc/self/fd/1 ... and you have a working filter. /proc/self/fd/N is approximately the same as /dev/fd/N in some UNIX and UNIX-like systems. Most Linux MAKEDEV scripts symbolically link /dev/fd to /proc/self/fd, in fact. Most systems provide symbolic links /dev/stdin, /dev/stdout, and /dev/stderr, which respectively link to the files 0, 1, and 2 in /proc/self/fd. Thus the example command above could be written as: $ foobar -i /dev/stdin -o /dev/stdout ... /proc/[pid]/fdinfo/ (since kernel 2.6.22) This is a subdirectory containing one entry for each file which the process has open, named by its file descriptor. The contents of each file can be read to obtain information about the corresponding file descriptor, for example: $ cat /proc/12015/fdinfo/4 pos: 1000 flags: 01002002 The pos field is a decimal number showing the current file offset. The flags field is an octal number that displays the file access mode and file status flags (see open(2)). The files in this directory are readable only by the owner of the process. /proc/[pid]/limits (since kernel 2.6.24) This file displays the soft limit, hard limit, and units of measurement for each of the process's resource limits (see getrlimit(2)). Up to and including Linux 2.6.35, this file is protected to only allow reading by the real UID of the process. Since Linux 2.6.36, this file is readable by all users on the system. /proc/[pid]/maps A file containing the currently mapped memory regions and their access permissions. The format is: address perms offset dev inode pathname 08048000-08056000 r-xp 00000000 03:0c 64593 /usr/sbin/gpm 08056000-08058000 rw-p 0000d000 03:0c 64593 /usr/sbin/gpm 08058000-0805b000 rwxp 00000000 00:00 0 40000000-40013000 r-xp 00000000 03:0c 4165 /lib/ld-2.2.4.so 40013000-40015000 rw-p 00012000 03:0c 4165 /lib/ld-2.2.4.so 4001f000-40135000 r-xp 00000000 03:0c 45494 /lib/libc-2.2.4.so 40135000-4013e000 rw-p 00115000 03:0c 45494 /lib/libc-2.2.4.so 4013e000-40142000 rw-p 00000000 00:00 0 bffff000-c0000000 rwxp 00000000 00:00 0 where "address" is the address space in the process that it occupies, "perms" is a set of permissions: r = read w = write x = execute s = shared p = private (copy on write) "offset" is the offset into the file/whatever, "dev" is the device (major:minor), and "inode" is the inode on that device. 0 indicates that no inode is associated with the memory region, as the case would be with BSS (uninitialized data). Under Linux 2.0 there is no field giving pathname. /proc/[pid]/mem This file can be used to access the pages of a process's memory through open(2), read(2), and lseek(2). /proc/[pid]/mountinfo (since Linux 2.6.26)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote