Important Announcement
PubHTML5 Scheduled Server Maintenance on (GMT) Sunday, June 26th, 2:00 am - 8:00 am.
PubHTML5 site will be inoperative during the times indicated!

Home Explore lab2

lab2

Published by vinasa67.vr, 2020-11-06 03:37:47

Description: lab2

Search

Read the Text Version

Network Programming Course Lab Handbook Lab 2: Linux OS environment This practical session elaborates the basic knowledge for building network application in Linux operating system including the process, thread, I/O and signal. 2.1 Process Process is an application that is currently executed by the operating system. Several of the commands are used for process operations. 1. ps-aux command can be used to determine which processes are being run by the Linux operating system as shown in Figure 2-1. Figure 2- 1. ps-aux | more Idawaty Ahmad, PhD 1

Network Programming Course Lab Handbook 2. Instructions kill [pid] can be used to terminate processes that are run by Linux as shown in Figure 2-2. Sleep process is terminated with the kill command 3084. Figure 2-2. kill command 3. The top command can be used to obtain current information about the processes, CPU and memory usage as shown in Figure 2-3. Figure 2-3. top 6. Service command can be used for checking the status information of a process. Figure 2-4 shows xinetd process with pid 2622 are being operated. Figure 2-4. service Idawaty Ahmad, PhD 2

Network Programming Course Lab Handbook 4. Instructions pstree can be used to determine the hierarchy of the child and the parent process as shown in Figure 2-5. Figure 2-5. pstree 5. Instruction jobs is used to determine the background process (background process) as shown in Figure 2-6. Xcalc process and simultaneously run xterm as a background process. & signs and used to run a process in the background. ps command is used to determine the identification numbers of those processes kill before deleted using the command as shown in Figure 2-6. Idawaty Ahmad, PhD 3

Network Programming Course Lab Handbook Figure 2-6. jobs Exercise: 2-1. Based on the examples, you are required to practice those commands. Display and verify the output. Name the file of the outputs as netbasic.docx. Upload it in Putrablast. (Individual) Idawaty Ahmad, PhD 4

Network Programming Course Lab Handbook 6. The two_process.c program in Figure 2-7 is an example of the use of system calls fork () to create a a child process as shown in Figure 2-8. The process ID (pid) for both processes can be found by using the system call getpid () and getppid (). The parent and child process is synchronized by using wait (). Figure 2-7 two_process.c Figure 2-8. The output of two_process.c Exercise: 2-2. Compile and execute two_process.c. Capture the output and state the process ID for the parent and child processes. Name the file of the output as netbasic.docx. Upload it in Putrablast. (Individual) Idawaty Ahmad, PhD 5

Network Programming Course Lab Handbook 2.2 Multithreading 7. The purpose of hellocancel.c program shown in Figure 2-9 and Figure 2-10 is to create two threads and each thread executes different function. The pthread_join() is used as synchronization points. A thread can be terminated via pthread_exit () and pthread_cancel(). The output is shown in Figure 2-11. Figure 2-9. hellocancel.c: main () function Idawaty Ahmad, PhD 6

Network Programming Course Lab Handbook Figure 2-10. hellocancel.c : related functions Figure 2-11. Output for hellocancel.c 2-3 Modify hellocancel.c so that each of the thread prints its thread identification number in their respective function. Name the file of the enhanced codes as hellocancel2.c, compile and execute it. Capture and verify the output. (Group) 2-4 Modify hellocancel.c so that Thread 1 display the current value of count variable as shown in Figure 2-12. Name the file of the enhanced codes as hellocancel3.c, compile and execute it. Capture and verify the output. (Group) Idawaty Ahmad, PhD 7

Network Programming Course Lab Handbook Figure 2-12. Output for hellojoin.c 8. The hellomutex.c program as shown in Figure 2-13 and Figure 2-14 uses a mutex to control two threads when executing printnum() function to display numbers. The output is shown in Figure 2-15. Figure 2-13. hellomutex.c: main () 8 Idawaty Ahmad, PhD

Network Programming Course Lab Handbook Figure 2-14. hellomutex.c: printnum () Figure 2-15. Output for hellomutex.c 2-5 Modify hellomutex.c to create four threads and each of these threads call printnum() function to display numbers. Name the file of the enhanced codes as hellomutex2.c, compile and execute it. Capture and verify the output. (Group) Idawaty Ahmad, PhD 9

Network Programming Course Lab Handbook 2.3 Input/Output 9. Compile and execute io.c file as shown in Figure 2-16. The objective of io.c is to copy all input from keyboard (i.e., standard input) to the screen (standard output) as shown in Figure 2-17 by using read () and write (). Figure 2-16. io.c Figure 2-17. Output for io.c 2-6. Modify io.c that uses for loop instead of while loop structure Name the file of the enhanced codes as io-for.c. Compile and execute io-for.c Display and verify the output. (Individual) Idawaty Ahmad, PhD 10

Network Programming Course Lab Handbook 10. By using text editor, build a file i.e., data.txt. The content of this file is Pengaturcaraan Rangkaian dalam Linux as shown in Figure 2-18. Figure 2-18. Content of data.txt The bukafail.c codes is to open data.txt file as shown in Figure 2-19. Compile and execute bukafail.c as shown in Figure 2-20. Figure 2-19. bukafail.c 11 Figure 2-20. Output for bukafail.c Idawaty Ahmad, PhD

Network Programming Course Lab Handbook 2-7. By using read () and write (), enhance the bukafail.c to count the number of characters data.txt file. Name the file of the enhance codes as kiraaksara.c. Compile and execute kiraaksara.c as shown in Figure 2-21. Display and verify the output. (Individual) Figure 2-21. Output for kiraaksara.c 2.4 Signal To be continued in the next lab session Idawaty Ahmad, PhD 12

Network Programming Course Lab Handbook Idawaty Ahmad, PhD 13


Like this book? You can publish your book online for free in a few minutes!
Create your own flipbook