• No se han encontrado resultados

2. INTRODUCCIÓN

2.2 Compromisos Adquiridos con la Autoridad Ambiental

2.2.3 Compromisos SEA-COR N° 54/2013

Algorithm:

1. Include <fcntl.h>, <sys/stat.h> and <unistd.h> along with other header files. 2. Accept filename and choice of operation from the user.

3. If choice is read, open file in mode O_RDONLY, read and display the contents and close file.

4. If choice is write, open file in mode O_WRONLY, accept data to be written and write into file and close file.

5. If choice is to append to file, open it in mode O_RDWR.Using lseek, position pointer to end of the file, accept data to be written and write into file and close file.

6. If choice is check file status, open file in any mode, use fstat () to get file status and display the same.

Test Input:

[root@localhost Programs]# gcc fileop.c [root@localhost Programs]#./a.out Enter the filename: /root/programs/test.c Enter choice:

Test Output: Choice: Write

Enter data: I am student of Pict, Pune. Data written

Choice: Append

Enter data: I am studying in BE. Data written

Choice: Read File contents are:

I am student of Pict, Pune. I am studying in BE. Choice File Status:

File Status is:

ID of the device containing the file: 2056 File serial number: 133702

Mode of file: 33188 Size in bytes: 80

Revised On: 22/06/2009 TITLE Signals PROBLEM STATEMENT /DEFINITION

Implement an Alarm clock application using signals

OBJECTIVE To understand the concept of signal.

• To implement Alarm clock using SIGALRM signal. S/W PACKAGES AND HARDWARE APPARATUS USED Linux Fedora 4

PC with the configuration as

Pentium IV 1.7 GHz. 128M.B RAM, 40 G.B HDD, 15’’Color Monitor, Keyboard, Mouse

REFERENCES Advanced Unix Programming By Richard Stevens Vijay Mukhi's -The 'c' odyssey UNIX- Gandhi

STEPS Refer to student activity flow chart, theory, algorithm, test input, test output INSTRUCTIONS FOR WRITING JOURNAL Title • Problem Definition • Theory • Algorithm • Source code • compilation steps • Output • Conclusion

Theory:

Signals, to be short, are various notifications sent to a process in order to notify it of various "important" events. By their nature, they interrupt whatever the process is doing at this minute, and force it to handle them immediately. Each signal has an integer number that represents it (1, 2 and so on), as well as a symbolic name that is usually defined in the file /usr/include/signal.h or one of the files included by it directly or indirectly (HUP, INT and so on. Use the command 'kill -l' to see a list of signals supported by your system).

Each signal may have a signal handler, which is a function that gets called when the process receives that signal. The function is called in "asynchronous mode", meaning that no where in your program you have code that calls this function directly. Instead, when the signal is sent to the process, the operating system stops the execution of the process, and "forces" it to call the signal handler function. When that signal handler function returns, the process continues execution from wherever it happened to be before the signal was received, as if this interruption never occurred.

Note for "hardwarists": If you are familiar with interrupts (you are, right?), signals are very similar in their behavior. The difference is that while interrupts are sent to the operating system by the hardware, signals are sent to the process by the operating system, or by other processes. Note that signals have nothing to do with software interrupts, which are still sent by the hardware (the CPU itself, in this case).

Signals are usually used by the operating system to notify processes that some event occurred, without these processes needing to poll for the event. Signals should then be handled, rather then used to create an event notification mechanism for a specific application.

When we say that "Signals are being handled", we mean that our program is ready to handle such signals that the operating system might be sending it (such as signals notifying that the user asked to terminate it, or that a network connection we tried writing into, was closed, etc). Failing to properly handle various signals, would likely cause our application to terminate, when it receives such signals.

The most common way of sending signals to processes is using the keyboard. There are certain key presses that are interpreted by the system as requests to send signals to the process with which we are interacting:

Ctrl-C

Pressing this key causes the system to send an INT signal (SIGINT) to the running process. By default, this signal causes the process to immediately terminate.

Ctrl-Z

Pressing this key causes the system to send a TSTP signal (SIGTSTP) to the running process. By default, this signal causes the process to suspend execution.

Ctrl-\

Pressing this key causes the system to send a ABRT signal (SIGABRT) to the running process. By default, this signal causes the process to immediately terminate. Note that this redundancy (i.e. Ctrl-\ doing the same as Ctrl-C) gives us some better flexibility. We'll explain that later on.

Another way of sending signals to processes is done using various commands, usually internal to the shell:

kill

The kill command accepts two parameters: a signal name (or number), and a process ID. Usually the syntax for using it goes something like:

kill -<signal> <PID>

For example, in order to send the INT signal to process with PID 5342, type: kill -INT 5342

This has the same affect as pressing Ctrl-C in the shell that runs that process.If no signal name or number is specified, the default is to send a TERM signal to the process, which normally causes its termination, and hence the name of the kill command.

fg

On most shells, using the 'fg' command will resume execution of the process (that was suspended with Ctrl-Z), by sending it a CONT signal.

A third way of sending signals to processes is by using the kill system call. This is the normal way of sending a signal from one process to another. This system call is also used by the 'kill' command or by the 'fg' command. Here is an example code that causes a process to suspend its own execution by sending itself the STOP signal:

#include <unistd.h> /* standard unix functions, like getpid() */ #include <sys/types.h> /* various type definitions, like pid_t */ #include <signal.h> /* signal name macros, and the kill() prototype */ /* first, find my own process ID */

pid_t my_pid = getpid();

kill(my_pid, SIGSTOP);

An example of a situation when this code might prove useful, is inside a signal handler that catches the TSTP signal (Ctrl-Z, remember?) in order to do various tasks before actually suspending the process. We will see an example of this later on.

Most signals may be caught by the process, but there are a few signals that the process cannot catch, and cause the process to terminate. For example, the KILL signal (-9 on all unices I've met so far) is such a signal. This is why you usually see a process being shut down using this signal if it gets "wild". One process that uses this signal is a system shutdown process. It first sends a TERM signal to all processes, waits a while, and after allowing them a "grace period" to shut down cleanly, it kills whichever are left using the KILL signal.

STOP is also a signal that a process cannot catch, and forces the process's suspension immediately. This is useful when debugging programs whose behavior depends on timing. Suppose that process A needs to send some data to process B, and you want to check some system parameters after the message is sent, but before it is received and processed by process B. One way to do that would be to send a STOP signal to process B, thus causing its suspension, and then running process A and waiting until it sends its oh- so important message to process B. Now you can check whatever you want to, and later on you can use the CONT signal to continue process B's execution, which will then receive and process the message sent from process A.

Now, many other signals are catchable, and this includes the famous SEGV and BUS signals. You probably have seen numerous occasions when a program has exited with a message such as 'Segmentation Violation - Core Dumped', or 'Bus Error - core dumped'. In the first occasion, a SEGV signal was sent to your program due to accessing an illegal memory address. In the second case, a BUS signal was sent to your program, due to accessing a memory address with invalid alignment. In both cases, it is possible to catch these signals in order to do some cleanup - kill child processes, perhaps remove temporary files, etc. Although in both cases, the memory used by your process is most likely corrupt, it's probable that only a small part of it was corrupt, so cleanup is still usually possible.

Algorithm:

1. Write the function to be invoked on the receipt of the signal.

2. Use the “signal” system call with SIGALARM in the signum field and address of function written in (1) as the function argument to register the user

function.

3. Accept number of seconds after which to signal the alarm.

4. Use alarm function with the number of seconds accepted in (3) to invoke the function written in (1) after the specified timestamp.

Test Input:

[root@localhost Programs]# gcc -o signal signals.c [root@localhost Programs]# ./signal

Enter the alarm interval in seconds: 5 Test Output: 1 2 3 4 ******** ALARM ********

Revised On: 22/06/2009 TITLE MultiThreading PROBLEM STATEMENT /DEFINITION

Create program which has three threads

1. Display Seconds 2. Display Minutes 3:Display Hours.and synchronize them

OBJECTIVE To understand the concept mutithreading.

• To implement digital clock by creating three threads and join them. S/W PACKAGES AND HARDWARE APPARATUS USED Linux Fedora 4

PC with the configuration as

Pentium IV 1.7 GHz. 128M.B RAM, 40 G.B HDD, 15’’Color Monitor, Keyboard, Mouse

REFERENCES Advanced Unix Programming By Richard Stevens

The Design of UNIX Operating System by Maurice Bach

STEPS Refer to student activity flow chart, theory, algorithm, test input, test output INSTRUCTIONS FOR WRITING JOURNAL Title • Problem Definition • Theory • Algorithm • Source code • compilation steps • Output • Conclusion

Theory:

We can think of a thread as basically a lightweight process. In order to understand this let us consider the two main characteristics of a process:

Unit of resource ownership -- A process is allocated:

• a virtual address space to hold the process image

• control of some resources (files, I/O devices...) Unit of dispatching

- A process is an execution path through one or more programs:

• execution may be interleaved with other processes

• the process has an execution state and a dispatching priority

If we treat these two characteristics as being independent (as does modern OS theory):

The unit of resource ownership is usually referred to as a process or task. This Processes have:

o a virtual address space which holds the process image.

o protected access to processors, other processes, files, and I/O resources. • The unit of dispatching is usually referred to a thread or a lightweight process.

Thus a thread:

o Has an execution state (running, ready, etc.) o Saves thread context when not running

o Has an execution stack and some per-thread static storage for local

variables

o Has access to the memory address space and resources of its process • all threads of a process share this when one thread alters a (non-private) memory

item, all other threads (of the process) sees that a file open with one thread, is available to others

Benefits of Threads vs Processes

If implemented correctly then threads have some advantages of (multi) processes, They take:

• Less time to create a new thread than a process, because the newly created thread uses the current process address space.

• Less time to terminate a thread than a process.

• Less time to switch between two threads within the same process, partly because the newly created thread uses the current process address space.

• Less communication overheads -- communicating between the threads of one process is simple because the threads share everything: address space, in particular. So, data produced by one thread is immediately available to all the other threads.

Example : A file server on a LAN

• It needs to handle several file requests over a short period

• Hence more efficient to create (and destroy) a single thread for each request

• Multiple threads can possibly be executing simultaneously on different processors Thread Levels

There are two broad categories of thread implementation:

• User-Level Threads -- Thread Libraries.

• Kernel-level Threads -- System Calls.

There are merits to both, in fact some OSs allow access to both levels (e.g. Solaris). User-Level Threads (ULT)

In this level, the kernel is not aware of the existence of threads -- All thread management is done by the application by using a thread library. Thread switching does not require kernel mode privileges (no mode switch) and scheduling is application specific

Kernel activity for ULTs:

• The kernel is not aware of thread activity but it is still managing process activity

• When a thread makes a system call, the whole process will be blocked but for the thread library that thread is still in the running state

• So thread states are independent of process states Advantages and inconveniences of ULT

Advantages:

• Thread switching does not involve the kernel -- no mode switching

• Scheduling can be application specific -- choose the best algorithm.

• ULTs can run on any OS -- Only needs a thread library Disadvantages:

• Most system calls are blocking and the kernel blocks processes -- So all threads within the process will be blocked

• The kernel can only assign processes to processors -- Two threads within the same process cannot run simultaneously on two processors

Kernel-Level Threads (KLT)

In this level, All thread management is done by kernel No thread library but an API (system calls) to the kernel thread facility exists. The kernel maintains context information for the process and the threads, switching between threads requires the kernel Scheduling is performed on a thread basis.

Advantages and inconveniences of KLT Advantages

• the kernel can simultaneously schedule many threads of the same process on many processors blocking is done on a thread level

• kernel routines can be multithreaded Disadvantages:

thread switching within the same process involves the kernel, e.g if we have 2 mode switches per thread switch this results in a significant slow down.

Algorithm:

1. Create three threads pertaining to the display of hour, minutes and seconds. 2. set the variables hh, mm and ss to the value corresponding to the local time. 3. Invoke the sleep function in each thread, handling function with arguments to

sleep depending upon the type of thread. Test Input:

[root@localhost Programs]# gcc gthread.c -lpthread [root@localhost Programs]# ./a.out

Test Output: 15:2:17 15:2:18 15:2:19 15:2:20 15:2:21

Revised On: 22/06/2009 TITLE Insertion of module in Kernel

PROBLEM

STATEMENT /DEFINITION

Write and insert module in Linux Kernel.

OBJECTIVE To implement program by writing module and insert it into kernel by using make file.

S/W PACKAGES AND HARDWARE APPARATUS USED Linux Fedora 4

PC with the configuration as

Pentium IV 1.7 GHz. 128M.B RAM, 40 G.B HDD, 15’’Color Monitor, Keyboard, Mouse

REFERENCES Linux Kernel Programming by Michael Beck

STEPS Refer to student activity flow chart, theory, algorithm, test input, test output INSTRUCTIONS FOR WRITING JOURNAL Title • Problem Definition • Theory • Algorithm • Source code • compilation steps • Output • Conclusion

Theory:

What exactly is a kernel module? Modules are pieces of code that can be loaded and unloaded into the kernel upon demand. They extend the functionality of the kernel without the need to reboot the system. For example, one type of module is the device driver, which allows the kernel to access hardware connected to the system. Without modules, we would have to build monolithic kernels and add new functionality directly into the kernel image. Besides having larger kernels, this has the disadvantage of requiring us to rebuild and reboot the kernel every time we want new functionality.

You can see what modules are already loaded into the kernel by running lsmod, which gets its information by reading the file /proc/modules.

How do these modules find their way into the kernel? When the kernel needs a feature that is not resident in the kernel, the kernel module daemon kmod execs modprobe to load the module in. modprobe is passed a string in one of two forms:

// modprobe is command used to load a single module in kernel

//Modprobe will automatically load all base modules needed in a module stack, as described by the dependency file modules.dep . If the loading of one of these modules fails, the whole current stack of modules loaded in the current session will be unloaded automatically.

• A module name like softdog or ppp.

• A more generic identifier like char-major-10-30.

If modprobe is handed a generic identifier, it first looks for that string in the file /etc/modprobe.conf.[2] If it finds an alias line like:

alias char-major-10-30 softdog

it knows that the generic identifier refers to the module softdog.ko.

Next, modprobe looks through the file /lib/modules/version/modules.dep, to see if other modules must be loaded before the requested module may be loaded. This file is created by depmod -a and contains module dependencies. For example, msdos.ko requires the fat.ko module to be already loaded into the kernel. The requested module has a dependency on another module if the other module defines symbols (variables or functions) that the requested module uses.

Lastly, modprobe uses insmod to first load any prerequisite modules into the kernel, and then the requested module. modprobe directs insmod to /lib/modules/version/[3], the standard directory for modules. insmod is intended to be fairly dumb about the location of modules, whereas modprobe is aware of the default location of modules, knows how to figure out the dependencies and load the modules in the right order. So for example, if you wanted to load the msdos module, you'd have to either run:

insmod /lib/modules/2.6.11/kernel/fs/fat/fat.ko

insmod /lib/modules/2.6.11/kernel/fs/msdos/msdos.ko

or:

modprobe msdos

What we've seen here is: insmod requires you to pass it the full pathname and to insert the modules in the right order, while modprobe just takes the name, without any extension, and figures out all it needs to know by parsing

/lib/modules/version/modules.dep.

Linux distros provide modprobe, insmod and depmod as a package called module-init- tools. In previous versions that package was called modutils. Some distros also set up some wrappers that allow both packages to be installed in parallel and do the right thing in order to be able to deal with 2.4 and 2.6 kernels. Users should not need to care about the details, as long as they're running recent versions of those tools.

Algorithm:

1. Write a .c program which consists of functionality that is to be implemented. 2. Write a make file.

3. Go to the directory where both these files are stored and run make command. Then .ko file will be created.

4. For inserting it use insmod. 5. To remove it use rmmod.

6. To check the message see file /log/messages or use dmesg command.

Test Input: .c file

Revised On: 22/06/2009 TITLE

Documento similar