Vcoderz Community
We create websites that have it all, beauty & brains
Lebanon Web Design & Development - Coddict
 

Go Back   Vcoderz Community > Computer Zone > Computers & Information Technologies

Notices

Computers & Information Technologies Everything related to computers and internet.

Reply
 
Share Thread Tools Search this Thread
Old 11-26-2010   #1
Sary
Registered Member
 
Sary's Avatar
 
Last Online: 11-08-2012
Join Date: Dec 2007
Posts: 113
Thanks: 77
Thanked 53 Times in 33 Posts
Groans: 0
Groaned at 0 Times in 0 Posts
Default Operating Systems: Communication via tubes & via signals !

Can anybody provide me with 2 codes about:
1- Communication via tubes
2- Communication via signals

and both should be in Linux !!
I need the codes written in C language of course .. with the ability to have their output ..

the codes shouldn't be so complicated .. I just need to understand the idea in the 2 concepts (tubes & signals) via these 2 codes !

Thanks in advance !


Sary is offline   Reply With Quote
Old 11-26-2010   #2
Tawa
 
Tawa's Avatar
 
Last Online: 06-12-2019
Join Date: Mar 2006
Posts: 6,245
Thanks: 2,120
Thanked 3,365 Times in 1,740 Posts
Groans: 29
Groaned at 44 Times in 35 Posts
Default

Tubes? you mean Pipes?
__________________
What we do in life, echoes in eternity.
Tawa is offline   Reply With Quote
The Following 2 Users Say Thank You to Tawa For This Useful Post:
Justin (11-27-2010), SysTaMatIcS (11-26-2010)
Old 11-27-2010   #3
Sary
Registered Member
 
Sary's Avatar
 
Last Online: 11-08-2012
Join Date: Dec 2007
Posts: 113
Thanks: 77
Thanked 53 Times in 33 Posts
Groans: 0
Groaned at 0 Times in 0 Posts
Default

Quote:
Originally Posted by Tawa View Post
Tubes? you mean Pipes?
Nope, dude ..
tubes are different from pipes !
I've got a code for the pipes concept ..
I need for the "tubes" and "signals" !
Sary is offline   Reply With Quote
Old 11-29-2010   #4
Google

 
Google's Avatar
 
Last Online: 05-30-2013
Join Date: Jan 2008
Posts: 1,788
Thanks: 10,018
Thanked 1,100 Times in 651 Posts
Groans: 1
Groaned at 6 Times in 6 Posts
Default

Quote:
Originally Posted by Sary View Post
Nope, dude ..
tubes are different from pipes !
I've got a code for the pipes concept ..
I need for the "tubes" and "signals" !
Un "tube" => |
A "pipe" => |


As for the signals, take a look at the kill command. It is an easy example:

Code:
/* kill -- send a signal to a process
   Copyright (C) 2002-2005, 2008-2010 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

/* Written by Paul Eggert.  */

#include <config.h>
#include <stdio.h>
#include <getopt.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/wait.h>

#include "system.h"
#include "error.h"
#include "sig2str.h"
#include "operand2sig.h"

/* The official name of this program (e.g., no `g' prefix).  */
#define PROGRAM_NAME "kill"

#define AUTHORS proper_name ("Paul Eggert")

#if ! (HAVE_DECL_STRSIGNAL || defined strsignal)
# if ! (HAVE_DECL_SYS_SIGLIST || defined sys_siglist)
#  if HAVE_DECL__SYS_SIGLIST || defined _sys_siglist
#   define sys_siglist _sys_siglist
#  elif HAVE_DECL___SYS_SIGLIST || defined __sys_siglist
#   define sys_siglist __sys_siglist
#  endif
# endif
# if HAVE_DECL_SYS_SIGLIST || defined sys_siglist
#  define strsignal(signum) (0 <= (signum) && (signum) <= SIGNUM_BOUND \
                             ? sys_siglist[signum] \
                             : 0)
# endif
# ifndef strsignal
#  define strsignal(signum) 0
# endif
#endif

static char const short_options[] =
  "0::1::2::3::4::5::6::7::8::9::"
  "A::B::C::D::E::F::G::H::I::J::K::L::M::"
  "N::O::P::Q::R::S::T::U::V::W::X::Y::Z::"
  "ln:s:t";

static struct option const long_options[] =
{
  {"list", no_argument, NULL, 'l'},
  {"signal", required_argument, NULL, 's'},
  {"table", no_argument, NULL, 't'},
  {GETOPT_HELP_OPTION_DECL},
  {GETOPT_VERSION_OPTION_DECL},
  {NULL, 0, NULL, 0}
};

void
usage (int status)
{
  if (status != EXIT_SUCCESS)
    fprintf (stderr, _("Try `%s --help' for more information.\n"),
             program_name);
  else
    {
      printf (_("\
Usage: %s [-s SIGNAL | -SIGNAL] PID...\n\
  or:  %s -l [SIGNAL]...\n\
  or:  %s -t [SIGNAL]...\n\
"),
              program_name, program_name, program_name);
      fputs (_("\
Send signals to processes, or list signals.\n\
\n\
"), stdout);
      fputs (_("\
Mandatory arguments to long options are mandatory for short options too.\n\
"), stdout);
      fputs (_("\
  -s, --signal=SIGNAL, -SIGNAL\n\
                   specify the name or number of the signal to be sent\n\
  -l, --list       list signal names, or convert signal names to/from numbers\n\
  -t, --table      print a table of signal information\n\
"), stdout);
      fputs (HELP_OPTION_DESCRIPTION, stdout);
      fputs (VERSION_OPTION_DESCRIPTION, stdout);
      fputs (_("\n\
SIGNAL may be a signal name like `HUP', or a signal number like `1',\n\
or the exit status of a process terminated by a signal.\n\
PID is an integer; if negative it identifies a process group.\n\
"), stdout);
      printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);
      emit_ancillary_info ();
    }
  exit (status);
}

/* Print a row of `kill -t' output.  NUM_WIDTH is the maximum signal
   number width, and SIGNUM is the signal number to print.  The
   maximum name width is NAME_WIDTH, and SIGNAME is the name to print.  */

static void
print_table_row (unsigned int num_width, int signum,
                 unsigned int name_width, char const *signame)
{
  char const *description = strsignal (signum);
  printf ("%*d %-*s %s\n", num_width, signum, name_width, signame,
          description ? description : "?");
}

/* Print a list of signal names.  If TABLE, print a table.
   Print the names specified by ARGV if nonzero; otherwise,
   print all known names.  Return a suitable exit status.  */

static int
list_signals (bool table, char *const *argv)
{
  int signum;
  int status = EXIT_SUCCESS;
  char signame[SIG2STR_MAX];

  if (table)
    {
      unsigned int name_width = 0;

      /* Compute the maximum width of a signal number.  */
      unsigned int num_width = 1;
      for (signum = 1; signum <= SIGNUM_BOUND / 10; signum *= 10)
        num_width++;

      /* Compute the maximum width of a signal name.  */
      for (signum = 1; signum <= SIGNUM_BOUND; signum++)
        if (sig2str (signum, signame) == 0)
          {
            size_t len = strlen (signame);
            if (name_width < len)
              name_width = len;
          }

      if (argv)
        for (; *argv; argv++)
          {
            signum = operand2sig (*argv, signame);
            if (signum < 0)
              status = EXIT_FAILURE;
            else
              print_table_row (num_width, signum, name_width, signame);
          }
      else
        for (signum = 1; signum <= SIGNUM_BOUND; signum++)
          if (sig2str (signum, signame) == 0)
            print_table_row (num_width, signum, name_width, signame);
    }
  else
    {
      if (argv)
        for (; *argv; argv++)
          {
            signum = operand2sig (*argv, signame);
            if (signum < 0)
              status = EXIT_FAILURE;
            else
              {
                if (ISDIGIT (**argv))
                  puts (signame);
                else
                  printf ("%d\n", signum);
              }
          }
      else
        for (signum = 1; signum <= SIGNUM_BOUND; signum++)
          if (sig2str (signum, signame) == 0)
            puts (signame);
    }

  return status;
}

/* Send signal SIGNUM to all the processes or process groups specified
   by ARGV.  Return a suitable exit status.  */

static int
send_signals (int signum, char *const *argv)
{
  int status = EXIT_SUCCESS;
  char const *arg = *argv;

  do
    {
      char *endp;
      intmax_t n = (errno = 0, strtoimax (arg, &endp, 10));
      pid_t pid = n;

      if (errno == ERANGE || pid != n || arg == endp || *endp)
        {
          error (0, 0, _("%s: invalid process id"), arg);
          status = EXIT_FAILURE;
        }
      else if (kill (pid, signum) != 0)
        {
          error (0, errno, "%s", arg);
          status = EXIT_FAILURE;
        }
    }
  while ((arg = *++argv));

  return status;
}

int
main (int argc, char **argv)
{
  int optc;
  bool list = false;
  bool table = false;
  int signum = -1;
  char signame[SIG2STR_MAX];

  initialize_main (&argc, &argv);
  set_program_name (argv[0]);
  setlocale (LC_ALL, "");
  bindtextdomain (PACKAGE, LOCALEDIR);
  textdomain (PACKAGE);

  atexit (close_stdout);

  while ((optc = getopt_long (argc, argv, short_options, long_options, NULL))
         != -1)
    switch (optc)
      {
      case '0': case '1': case '2': case '3': case '4':
      case '5': case '6': case '7': case '8': case '9':
        if (optind != 2)
          {
            /* This option is actually a process-id.  */
            optind--;
            goto no_more_options;
          }
        /* Fall through.  */
      case 'A': case 'B': case 'C': case 'D': case 'E':
      case 'F': case 'G': case 'H': case 'I': case 'J':
      case 'K': case 'L': case 'M': case 'N': case 'O':
      case 'P': case 'Q': case 'R': case 'S': case 'T':
      case 'U': case 'V': case 'W': case 'X': case 'Y':
      case 'Z':
        if (! optarg)
          optarg = argv[optind - 1] + strlen (argv[optind - 1]);
        if (optarg != argv[optind - 1] + 2)
          {
            error (0, 0, _("invalid option -- %c"), optc);
            usage (EXIT_FAILURE);
          }
        optarg--;
        /* Fall through.  */
      case 'n': /* -n is not documented, but is for Bash compatibility.  */
      case 's':
        if (0 <= signum)
          {
            error (0, 0, _("%s: multiple signals specified"), optarg);
            usage (EXIT_FAILURE);
          }
        signum = operand2sig (optarg, signame);
        if (signum < 0)
          usage (EXIT_FAILURE);
        break;

      case 't':
        table = true;
        /* Fall through.  */
      case 'l':
        if (list)
          {
            error (0, 0, _("multiple -l or -t options specified"));
            usage (EXIT_FAILURE);
          }
        list = true;
        break;

      case_GETOPT_HELP_CHAR;
      case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
      default:
        usage (EXIT_FAILURE);
      }
 no_more_options:;

  if (signum < 0)
    signum = SIGTERM;
  else if (list)
    {
      error (0, 0, _("cannot combine signal with -l or -t"));
      usage (EXIT_FAILURE);
    }

  if ( ! list && argc <= optind)
    {
      error (0, 0, _("no process ID specified"));
      usage (EXIT_FAILURE);
    }

  return (list
          ? list_signals (table, optind < argc ? argv + optind : NULL)
          : send_signals (signum, argv + optind));
}
__________________


Last edited by Google; 11-29-2010 at 12:29 AM.
Google is offline   Reply With Quote
Old 11-30-2010   #5
Sary
Registered Member
 
Sary's Avatar
 
Last Online: 11-08-2012
Join Date: Dec 2007
Posts: 113
Thanks: 77
Thanked 53 Times in 33 Posts
Groans: 0
Groaned at 0 Times in 0 Posts
Default

Google

Dude, thanks a lot for the code ..
but the code is a bit complicated especially that our BELOVED Uni gave us the 1st programming course in C++ language .. and now in the OS course .. the language is C !!

in addition, most of the functions used in the code u provided are new .. although I don't care too much for the material written as much as I want to expalin the "signal" concept via this code through explaining what this code does ! .. so can I have a brief explanation of this code ? and can I run it on Fedora (Linux) in order to have its output ?!
Sary is offline   Reply With Quote
Old 11-30-2010   #6
Google

 
Google's Avatar
 
Last Online: 05-30-2013
Join Date: Jan 2008
Posts: 1,788
Thanks: 10,018
Thanked 1,100 Times in 651 Posts
Groans: 1
Groaned at 6 Times in 6 Posts
Default

Quote:
Originally Posted by Sary View Post
Google

Dude, thanks a lot for the code ..
but the code is a bit complicated especially that our BELOVED Uni gave us the 1st programming course in C++ language .. and now in the OS course .. the language is C !!

in addition, most of the functions used in the code u provided are new .. although I don't care too much for the material written as much as I want to expalin the "signal" concept via this code through explaining what this code does ! .. so can I have a brief explanation of this code ? and can I run it on Fedora (Linux) in order to have its output ?!
There is no big difference between C and C++. You have to depend on yourself to learn. You cannot rely on the university to give you everything. Anyway, this code was taken from the GNU core-utils. It is for the kill command that is used on UNIX or LINUX systems. It is like taskkill that is used on Windows. This code is basically a wrapper for the system function kill. The most important part of it is this line:
Code:
else if (kill (pid, signum) != 0)
The kill system function is used to send a signal to a process.
Take a look here => http://linux.die.net/man/7/signal on the types of signals that may be sent to a certain process. Note that, each process is represented by a process id.
You can compile C on Fedora but you need gcc installed on the system. (sudo -c 'yum install gcc')
__________________

Google is offline   Reply With Quote
Old 12-01-2010   #7
Sary
Registered Member
 
Sary's Avatar
 
Last Online: 11-08-2012
Join Date: Dec 2007
Posts: 113
Thanks: 77
Thanked 53 Times in 33 Posts
Groans: 0
Groaned at 0 Times in 0 Posts
Default

yeah I know about the GCC thing ..
but what about the "signum" signal ? what is its role ?
as I see from the most important line u posted .. is that u kill a process by sending a signal to that process via its ID .. is that the whole idea ?

In addition dude .. did u say before that communication via pipes is the same as communication via tubes ?!

Thanks a lot for what you have provided up till now ..
Sary is offline   Reply With Quote
Old 12-01-2010   #8
Google

 
Google's Avatar
 
Last Online: 05-30-2013
Join Date: Jan 2008
Posts: 1,788
Thanks: 10,018
Thanked 1,100 Times in 651 Posts
Groans: 1
Groaned at 6 Times in 6 Posts
Default

Quote:
Originally Posted by Sary View Post
yeah I know about the GCC thing ..
but what about the "signum" signal ? what is its role ?
as I see from the most important line u posted .. is that u kill a process by sending a signal to that process via its ID .. is that the whole idea ?

In addition dude .. did u say before that communication via pipes is the same as communication via tubes ?!

Thanks a lot for what you have provided up till now ..
You're asking about signum, but I told you to look at this page to know what is it. It is basically a signal type. For more reference read here.
And yes, pipes are "tubes". Do you have other definition for tubes?
__________________

Google is offline   Reply With Quote
Reply

  Vcoderz Community > Computer Zone > Computers & Information Technologies

Tags
communication, operating, signals, systems, tubes



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:03 PM.


Lebanon web design and development
Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Share