Unix Basics Part 14 | multithreading | sleep | wait | How to do multithreading in Unix

Поділитися
Вставка
  • Опубліковано 25 тра 2024
  • To start with multithreading first we need to understand process commands.
    There are different process commands in Unix. Below are widely used.
    ps - process status, displays information about the active processes.
    sleep - This command is used to delay the execution, in given number of seconds, hours, minutes, days etc...
    wait - This command will suspend execution of the parent thread until one of its children terminate.
    kill - This is used to terminate a background running process. It takes process ID as an argument with
    multiple options to pass.
    exit - command is used to exit from the current shell environment.
    sleep:
    This command is used to delay the execution, in given number of seconds, hours, minutes, days
    This command is generally used to run a command periodically, like once every 1 minute etc.
    If you have to do some operation like once in a day, between we can use cron (a command-line utility
    for job scheduling ).
    $ echo "Before Date "; date; sleep 10; echo "After Date "; date
    s This is used for specifying seconds.
    h This is used for specifying hours.
    d This is used for specifying days.
    1m = 1 minute
    1h = 1 hour
    1d = 1 day
    .1 = 1/10th of a second
    .01 = 1/100th of a second
    $ while true
    do
    echo “Current Date........ "
    date
    sleep 10
    done
    Wait
    This command will suspend execution of the parent thread until one of its children terminate.
    wait normally returns the exit status of the last job which terminated.
    wait (process or job ID) An added PID or job ID waits for a specific process to end before continuing the script.
    wait -n Waits for only the following background process to complete and returns an exit status.
    wait -f Waits for all the background process, signifies to wait until the
    specified job or process terminates, instead of waiting until it changes
    state(like a failure).
    & This is known as job control under Unix. It informs the shell to put the command in the background.
    jobs Use to check for running process.
    test_multithread.sh
    #!/bin/bash
    echo "process 1 started!"
    date
    sleep 10 &
    echo "-------------"
    echo "process 2 started!"
    date
    sleep 10 &
    echo "-------------"
    echo "process 3 started!"
    date
    sleep 20 &
    echo "-------------"
    echo "process 4 started!"
    date
    sleep 20 &
    echo "-------------"
    echo "process 5 started!"
    date
    sleep 10 &
    echo "-------------"
    wait %1
    echo "Process 1 completed!"
    wait %2
    echo "process 2 completed!"
    wait
    echo "-------------"
    echo "All jobs are finished!"
    date
    ---------------------------------
    #unixbasics #sleep #unix #unixtutorial #sleepcommand #unix #unixtutorial #sleep #learning #coding #programming

КОМЕНТАРІ •