SourceForge.net Logo
/* Copyright (c) GPL 2004  mmc  Mike Chirico mchirico@users.sourceforge.net

System V Semaphores

   This is a demonstration of System V Semaphores under Linux, which
   handles semadj, per process adjustment value, uniquely when the
   SEM_UNDO flag is set.  Basically,  the semaphore value is decressed 
   as far as possible (i.e., to zero but not below) when the process
   terminates. This makes for some interesting results.  The semval
   can increase in certain situations.
   

   Compile:
   You'll need to see the Makefile and other related files in
Ref:
   http://cvs.sourceforge.net/viewcvs.py/cpearls/cpearls/src/development/semaphore/test/
*/
#include "msema.h"
#include <getopt.h>


using namespace std;

#define BUFFER "./buffer"
#define MAX_TRIES 3
#define NUM_SEMS 3

struct sembuf lock_sem, unlock_sem;
struct semid_ds seminfo;
extern int errno;


/*
  process_z_opt gets the threads waiting for zerio getzcnt and the 
  number of threads waiting getncnt
*/
int
process_z_opt (Sem & p)
{
  int i;
  for (i = 0; i < 3; ++i)
    {
      cout << "p.Getzcnt(" << i << ")=  " << p.Getzcnt (i) << "  ";
      cout << "\tp.Getncnt(" << i << ")=  " << p.Getncnt (i) << endl;
    }
  return 1;
}

int
process_k_opt (Sem & p)
{
  p.Semget (0, 0);
  p.Semctl (0, IPC_RMID);
  return 1;
}

int
process_s_opt (Sem & p, unsigned short *&ptr,
	       union semun &arg, struct semid_ds &seminfo)
{

  int nsems;
  int i;

  p.Semget (0, 0);
  arg.buf = &seminfo;
  p.Semctl (0, IPC_STAT, arg);
  nsems = arg.buf->sem_nsems;
  if ((ptr = (unsigned short *) calloc (nsems,
					sizeof (unsigned short))) == NULL)
    {
      fprintf (stderr, "calloc error \n");
      exit (-1);
    }
  arg.array = ptr;
  p.Semctl (0, GETALL, arg);
  for (i = 0; i < nsems; ++i)
    printf ("semval[%d] = %d\n", i, ptr[i]);
  free (ptr);
  cout << " Num sems: " << nsems << endl;
  return 0;
}

int
process_o_opt (int beg,int end,char **argv)
{
  for(int i=beg; i < end; ++i)
    cout << " " << argv[i];

  return 1;
}

int
main (int argc, char **argv)
{


  int c;
  unsigned short *ptr;
  char *opt = "skczoh";
  union semun arg;
  struct semid_ds seminfo;

  Sem p;

  while ((c = getopt (argc, argv, opt)) != -1)
    {
      switch (c)
	{
	case 's':
	  process_s_opt (p, ptr, arg, seminfo);
	  break;
	case 'k':
	  process_k_opt (p);
	  break;
	case 'u':
	  cout << " u state" << endl;
	  break;
	case 'c':
	  p.Semget (3, IPC_CREAT | 0600);
	  break;
	case 'z':
	  process_z_opt (p);
	  break;
	case 'o':
	  process_o_opt (optind,argc,argv);
	  argv[optind]='\0';
	  argc=0;

	  break;
	case 'h':
	  cout <<
	    " semastat <-skuh> <num> s stat, k kill, u update, c create, h help"
	    << endl;
	  exit (0);
	  break;
	default:
	  printf ("?? getopt returned character code 0%o ??\n", c);
	  exit (0);
	  break;

	}
    }



}



Tutorials

Linux System Admin Tips: There are over 200 Linux tips and tricks in this article. That is over 100 pages covering everything from NTP, setting up 2 IP address on one NIC, sharing directories among several users, putting running jobs in the background, find out who is doing what on your system by examining open sockets and the ps command, how to watch a file, how to prevent even root from deleting a file, tape commands, setting up cron jobs, using rsync, using screen conveniently with emacs, how to kill every process for a user, security tips and a lot more. These tip grow weekly. The above link will download the text version for easy grep searching. There is also an html version here.

Breaking Firewalls with OpenSSH and PuTTY: If the system administrator deliberately filters out all traffic except port 22 (ssh), to a single server, it is very likely that you can still gain access other computers behind the firewall. This article shows how remote Linux and Windows users can gain access to firewalled samba, mail, and http servers. In essence, it shows how openSSH and Putty can be used as a VPN solution for your home or workplace.

MySQL Tips and Tricks: Find out who is doing what in MySQL and how to kill the process, create binary log files, connect, create and select with Perl and Java, remove duplicates in a table with the index command, rollback and how to apply, merging several tables into one, updating foreign keys, monitor port 3306 with the tcpdump command, creating a C API, complex selects, and much more.

Create a Live Linux CD - BusyBox and OpenSSH Included: These steps will show you how to create a functioning Linux system, with the latest 2.6 kernel compiled from source, and how to integrate the BusyBox utilities including the installation of DHCP. Plus, how to compile in the OpenSSH package on this CD based system. On system boot-up a filesystem will be created and the contents from the CD will be uncompressed and completely loaded into RAM -- the CD could be removed at this point for boot-up on a second computer. The remaining functioning system will have full ssh capabilities. You can take over any PC assuming, of course, you have configured the kernel with the appropriate drivers and the PC can boot from a CD. This tutorial steps you through the whole processes.

SQLite Tutorial : This article explores the power and simplicity of sqlite3, first by starting with common commands and triggers, then the attach statement with the union operation is introduced in a way that allows multiple tables, in separate databases, to be combined as one virtual table, without the overhead of copying or moving data. Next, the simple sign function and the amazingly powerful trick of using this function in SQL select statements to solve complex queries with a single pass through the data is demonstrated, after making a brief mathematical case for how the sign function defines the absolute value and IF conditions.

The Lemon Parser Tutorial: This article explains how to build grammars and programs using the lemon parser, which is faster than yacc. And, unlike yacc, it is thread safe.

How to Compile the 2.6 kernel for Red Hat 9 and 8.0 and get Fedora Updates: This is a step by step tutorial on how to compile the 2.6 kernel from source.

Virtual Filesystem: Building A Linux Filesystem From An Ordinary File. You can take a disk file, format it as ext2, ext3, or reiser filesystem and then mount it, just like a physical drive. Yes, it then possible to read and write files to this newly mounted device. You can also copy the complete filesystem, since it is just a file, to another computer. If security is an issue, read on. This article will show you how to encrypt the filesystem, and mount it with ACL (Access Control Lists), which give you rights beyond the traditional read (r) write (w) and execute (x) for the 3 user groups file, owner and other.

Working With Time: What? There are 61 seconds in a minute? We can go back in time? We still tell time by the sun?



Chirico img Mike Chirico, a father of triplets (all girls) lives outside of Philadelphia, PA, USA. He has worked with Linux since 1996, has a Masters in Computer Science and Mathematics from Villanova University, and has worked in computer-related jobs from Wall Street to the University of Pennsylvania. His hero is Paul Erdos, a brilliant number theorist who was known for his open collaboration with others.


Mike's notes page is souptonuts. For open source consulting needs, please send an email to mchirico@gmail.com. All consulting work must include a donation to SourceForge.net.

SourceForge.net Logo


SourceForge.net Logo