Lab 5
Lab 5
Lab 5
Description. In the first task of this lab, you will learn how to create Unix cron
jobs to schedule arbitrary tasks for execution at specific dates and times. In the sec-
ond task, you will learn how to use Java threads to develop multi-threaded programs.
In the last task, you will learn how to use pipes for inter-process communication.
Task 1. cron is a Unix “deamon” program that allows you to schedule and to auto-
mate tasks (known as cron jobs) that need to be executed periodically to maintain a
good working and flexible system. Such tasks include, but are not limited to, deleting
old log files, sending out notification emails (e.g., newsletters and password expira-
tion emails), and regular clean-up of cached data. Most Unix-like operating systems
provide the crontab utility to create and schedule cron jobs.
crontab is the file (Located at /var/spool/cron/crontabs/<username>) where
you define (in a tabular style) what tasks to run and how often to run them. The
crontab also represents the command to be used to open the crontab file. A cron
job is an entry in the crontab file. It is defined in the form:
minute hour day month week command
0-59 0-23 1-31 1-12 1-7 script
You can start by checking the status of the cron process as it should run in the
background (as a deamon): sudo systemctl status cron. In the case where the
service is not running, you may enable it with: sudo systemctl enable cron and
then start the service with sudo systemctl start cron.
To create a corn job, use the crontab -e command to edit the crontab file and
add the following two lines:
* * * * * ps -aux >> ∼/processes log
0 8 * * 1-5 firefox https://www.news.google.com
0 0 * * 1 curl https://www.google.com >> page.html
In the case cron is not installed, install it with sudo apt install cron -y.
Copyright © Dr. Karim Lounis, 2022/2023.
Task 2. In this task, you will write your first multi-threaded Java program.
As we have seen in Lecture 4, a thread is a lightweight process. It can be seen
as an execution flow of a given program. A program may have multiple execution
flows (multi-threaded program). A thread shares with the main process (as well as
with other threads from the same process) the code section, global data section, heap
section, as well as the resources such as open files. Yet, it has a private stack section
and private CPU-register values.
Threads allow you to develop multi-threaded applications to run on multi-core CPUs.
This would improve applications’ response and execution time, used memory space,
and performance.
In Java, the simplest way to use threads is to start by defining a Java class that inherits
from the class java.lang.Thread. In the example below (MyThread.java), the
new Java class is called MyThread. Next you should redefine the body of the method
run() to include the code that will be executed by the thread.
class MyThread extends Thread
{
public void run ()
{
//code to be executed by any thread
}
}
To create and execute a thread, you must create an instance of the class MyThread (see
class above) and then call the method start() as shown below (ThreadExample.java)
class ThreadExample
{
public static void main (String[] args)
{
MyThread t 1 = new MyThread();
t 1.start();
}
}
pipe (Ext);
On success, the pipe() function returns 0, otherwise, it returns -1. The pipe()
function takes an array of two integers as an argument. The first element Ext[0] is
used for reading from the pipe, whereas the second element Ext[1] is used for writing
to the pipe. The latter should be closed by the reader process when about to read.
Similarly, the writer process would close Ext[0] when about to write to the pipe.
If a process wants to write a message msg (a string) to the pipe, it calls:
If it wants to read a message from the pipe, it calls (where buffer is an array of
characters of size buffer-size):