6
HOMEWORK-2 Most of this homework (1,2,3) requires you to take pictures of your software environment using Print Screen (search in google how to use Print Screen if you do not know). 4, 5 questions require you to submit the code and output. 6 question require you to write at least 200 words. Submit your answers in a single DOC or PDF file and save that file with your IDs and group number (42901234_42905678_2). You can do this homework in a group of 2. Due Date: 14/November/2012 29/12/1433 PART-1 Setting up an Environment for LINUX System Programming Installing Linux (0 marks) In this homework you will install Ubuntu Linux on your computer. You will need at least 10GB free hard disk space (20GB preferred). Download the latest 32-bit version of Ubuntu from www.ubuntu.com, and save it somewhere on your computer. You have two choices for installation. You can install in VMWare, or create separate partitions on your hard disk. Either way is good, but I’ve explained VMWare below. However, if you choose to create partitions on your hard disk, most of the instructions below will be useful for you as well. VMWare allows you to run a second operating system inside a window. For example, you can run Windows normally, and inside a window you can run a different operating system, just like running Microsoft Word in a window. See the picture below. In this case, Windows is the host OS, and Linux is the guest OS. Download and install the latest VMWare Player (ver. 3.0.1) from www.vmware.com. This is free for Windows, but you have to register. Mac OS users will have to buy a license for VMWare Fusion. However, Mac OS users should be able to run parallel code with little changes so it is not so important for them to install VMWare and Ubuntu.

Homework 22

Embed Size (px)

Citation preview

Page 1: Homework 22

HOMEWORK-2 Most of this homework (1,2,3) requires you to take pictures of your software environment using Print Screen (search in google how to use Print Screen if you do not know). 4, 5 questions require you to submit the code and output. 6 question require you to write at least 200 words. Submit your answers in a single DOC or PDF file and save that file with your IDs and group number (42901234_42905678_2). You can do this homework in a group of 2.

Due Date: 14/November/2012 29/12/1433 PART-1 Setting up an Environment for LINUX System Programming

Installing Linux (0 marks)

In this homework you will install Ubuntu Linux on your computer. You will need at least 10GB free hard disk space (20GB preferred). Download the latest 32-bit version of Ubuntu from www.ubuntu.com, and save it somewhere on your computer.

You have two choices for installation. You can install in VMWare, or create separate partitions on your hard disk. Either way is good, but I’ve explained VMWare below. However, if you choose to create partitions on your hard disk, most of the instructions below will be useful for you as well.

VMWare allows you to run a second operating system inside a window. For example, you can run Windows normally, and inside a window you can run a different operating system, just like running Microsoft Word in a window. See the picture below. In this case, Windows is the host OS, and Linux is the guest OS. Download and install the latest VMWare Player (ver. 3.0.1) from www.vmware.com. This is free for Windows, but you have to register. Mac OS users will have to buy a license for VMWare Fusion. However, Mac OS users should be able to run parallel code with little changes so it is not so important for them to install VMWare and Ubuntu.

Page 2: Homework 22

Once you have installed VMWare Player, it will ask you to restart. Go ahead and restart. After the restart, run VMWare Player. Choose “Create a New Virtual Machine”. Choose “Installer disc image file (iso)”, and browse to where you saved the Ubuntu ISO. Click “Next”, enter your preferred username details, click “Next”, change the location if needed (remember you need at least 10GB free), click “Next”, if you do not have 20GB free, then reduce the disk size, click “Next”, click “Customize Hardware...”, and change “Processors” to match the number of cores in your computer. My laptop has two cores, so I changed this to 2. Click “OK”, and then “Finish”. A window will appear, and it will start installing Ubuntu. Wait for this to finish (warning: it will take a long time).

When the installation is complete, the login screen will appear. Login to Ubuntu. Congratulations! You have now installed Linux on your computer! Now let’s learn some basic things inside Linux.

Check your Settings

In this section we check to see if your system is ready to make C programs, and that it can connect to the network/internet.

Click on the circle in the top-left corner (this is the “Start” button in Ubuntu). Go to Accessories -> Terminal (see picture below).

Page 3: Homework 22

1. Connect to the internet. In the terminal, type ping www.google.com and press Enter. You should see output similar to the picture below, which shows that your network settings are correct. Press Ctrl+c to stop the program. Submit a picture of the results using Print Screen. [1 mark]

The rest of these questions check your software setup. If you do not get the expected answers, or you get an error message, then you have made a mistake when installing Linux

Page 4: Homework 22

2. In the terminal, type gcc -v and press Enter. This will show your gcc settings. Gcc is a C compiler (and much more) for Linux. Make sure it is version 4.4. or higher, and “Thread model: POSIX”.Take a picture using Print Screen. [1 mark]

3. Use the information from cat /proc/cpuinfo to identify your processor model. For example, my processor model is Intel T7250. Google for your processor model and find out the following details: a. Speed of Front Side Bus (FSB) [1 mark]. b. Size of L1 cache [0.5 mark] c. Size of L2 cache [0.5 mark]

Page 5: Homework 22

PART-2 Programming Assignment

In part-1 you ran some commands in Terminal. It loads a shell environment, which is a powerful tool for managing and processing files (and other things). Before we start, you should know that Linux is case-specific. For example, if you save a file with the name “homework.doc”, then the name “Homework.doc” is not the same, because of the capital H. Windows is not like this, but Linux is. In Linux, if you tried to open “Homework.doc” it would say there is no file with that name, but in Windows it would open the file “homework.doc”.

The first command to learn is pwd. This stands for present working directory and shows you the name of the directory that you are currently in. A directory is like a folder in Windows. If you open Terminal, type pwd, and press Enter, it will print /home/[username] where [username] is the username you selected when you installed Linux. This is your home directory. Whenever you start Terminal, it will start at your home directory.

The second command to learn is ls. In Terminal type ls, and press Enter. You will see the names of some directories. This command is used to list all the files and directories that are present in the current directory. But it is more powerful than that. For example, if you type “ls D*” it will only print directories that start with “D”. There are also many options for ls such as “-l”. If you enter the command “ls -l” it will list the files and directories with more detailed information.

The third important command is mkdir. This is used to make new directories. Let’s make a new director called “Code”. Open Terminal, and type “mkdir Code”, press Enter. Now type ls and press Enter. You will see a new directory called Code.

“cd Code”. This will change directory. Now if you enter pwd you will see the present working directory is /home/[username]/Code. Here are some quick tips with cd. First, if you type “cd ..” it will go up one directory. For example, if you are in /home/[username]/Code, and you type “cd ..” you will go to /home/[username].

Page 6: Homework 22

How to write, compile and run ‘C’ Program #include<stdio.h> // This is a header file, it is just like a import in java int main() // This is a main function { printf(“Hello World”); //To print Hello World on output screen return 0; } Save the above four lines code in a file “test.c” Compile: GCC (C compiler for LINUX), “gcc test.c” compile the file. On successful compilation (without error), it will create an output file a.out Run: “./a.out” It will execute the file Process creation and signal handling Goal: The goal of this program is to help you understand the process management concepts in LINUX/UNIX. Specially, you learn the usage of the following system calls:

• fork(): process creation • exec(): replacing a process with another program • kill(): sending a signal to a process • signal(): registering/installing signal catch routines.

4. Write a program that create two new process using fork() system call and replacing one of a process with another program using exec() or execlp() system call. [2 mark] Hint: execlp("/bin/ls", "ls", NULL); // ls command will print list of all files and directories in your current folder. 5. Write a code using kill() system call to send a STOP signal to itself. Hint: pid_t my_pid = getpid(); // Process gets its own Id. [1 mark] kill(my_pid, SIGSTOP); //sending STOP signal to my_pid process Id. Multithreading 6. What are the four points that you kept in mind while designing parallel program? Explain each of them. [2 mark]