operating-system,semaphore , What happens when Binary Semaphore is Signalled twice? ie. s=1;wait(s); signal(s)signal(s); Does s becomes 0 or it remains 1?


What happens when Binary Semaphore is Signalled twice? ie. s=1;wait(s); signal(s)signal(s); Does s becomes 0 or it remains 1?

Question:

Tag: operating-system,semaphore

What happens when Binary Semaphore is Signalled twice? i.e. Suppose s is binary semaphore variable with s=1

Now we do following--

wait(s),signal(s),signal(s)

Does s becomes 0 or it remains 1?


Answer:

The binary semaphore would be used to protect some resource, so that only one process may access it at a time.

This process would do a wait, which, assuming the semaphore is 1 (resource isn't already busy) would make the semaphore =0.

When that process is finished with the resource it would signal, allowing other processes access to the resource, making the semaphore =1.

There's no reason why the process would signal twice, nor is there any reason why any other process would signal before doing a wait (let me know if you've thought of one), so two successive signals would be an error. Now if I was writing the OS primitive implementation I'd judge it was a better idea to leave the signal at 1 than to put it to 0 and probably lock the resource out for all time.

The standard texts on semaphores talk about incrementing and decrementing the count, but that's because it's a useful way of thinking about counting semaphores. The way actual semaphores behave in an actual OS when they're used in an odd way depends on the implementation, and is probably not worth examining as a general theory.


Related:


Microcontroller specific codes in OS (Contiki)


operating-system,contiki
I am working on creating a port for LPC1347 to contiki-os. I have 2 questions: 1: What i do not understand is how would the os know what architecture the hardware follows ie, whether is Harvard architecture or Neumann architecture. The microcontroller has 2 srams, so does the compiler or...

How does the OS decide data that goes in each page?


operating-system,disk
I have a comma separated data file, lets assume each record is of fixed length. How does the OS(Linux) determine, which data parts are kept in one page in the hard disk? Does it simply look at the file, organize the records one after the other(sequentially) in one page? Is...

Switch to user space


assembly,x86,operating-system,kernel
this is part of code which demonstrates basic functions of os kernel. After flushing GTD and setting up IDT I want to switch to ring 3 run some int and context switch. However I cannot jump to user mode. I want to use trick with iret instruction. As far I...

How to change ubuntu to root?


linux,permissions,operating-system
I am new to linux. when i list the files under folder 1, i see below result -rwxrwxrwx 1 root root 920 Jun 3 10:36 file1.xsd -rwxrwxrwx 1 root root 920 Jun 3 10:36 file2.xsd drwxrwxrwx 2 ubuntu ubuntu 4096 Jun 12 11:10 temp -rwxrwxrwx 1 ubuntu ubuntu 1853 Jun...

Semaphore doesn't seem to work correctly with multiple threads


c,multithreading,semaphore
I try to write multiple threading example with C. Which is the gets number from user and sum numbers in two thread. Threads are directly away each other, front to back and back to front. I am using semaphore to manage global variables but output shows that semaphore do not...

Bound wait to solve race condition


algorithm,process,operating-system,synchronization,race-condition
I am trying to Give a race condition example , then write an algorithm to impose synchronization and write an algorithm that implement the Bounded wait solution?! I tried the case of when two admins A and B in the school receive 2 students to register them if they hit...

Synchronizing processes with semaphores and signals in C


c,linux,multiprocessing,signals,semaphore
I have to write program in C on Linux. It has to have 3 processes - first reads from STDIN, sends message through FIFO to second process, which counts lenght of recevied message and sends result to third process (also through FIFO), which displays it on STDOUT. I have to...

How to translate these logical addresses into physical addresses?


memory-management,operating-system
Given that: Logical addresss space=64 bits Physical address space=32 bits Page size=4K How will the logical addresses 20, 4100 and 8300 be mapped to physical address for the following page table? ...

Android - What is the difference between a foreground and background service?


android,operating-system
From android.com it says that a foreground service is something that the user is aware of and a foreground service is unlikely to be killed if memory is needed. It says a background service may be killed if memory is needed. Is this the only difference or can one service...

What happens when Binary Semaphore is Signalled twice? ie. s=1;wait(s); signal(s)signal(s); Does s becomes 0 or it remains 1?


operating-system,semaphore
What happens when Binary Semaphore is Signalled twice? i.e. Suppose s is binary semaphore variable with s=1 Now we do following-- wait(s),signal(s),signal(s) Does s becomes 0 or it remains 1?...

Not checking file size correctly


python,file,python-3.x,file-io,operating-system
I wrote a simple script to move movies in my downloads directory, once they are finished downloading, to the location they need to go and have a simple question regarding it. Maybe I'm not understanding how the os module is working to the current size of the file here, but...

Not Used Recently (NUR) Page Replacement Algorithm [closed]


java,c++,c,operating-system
How to implement NUR (Not used recently page replacement algorithm) using any high level programming language (c, c++ or java)

Implementing a simple spin lock given some instruction


c,operating-system
For instance if have the following code which is some If set then clear and performs atomic in following manner. This is simple C code. int IfSetThenClear(int * ptr) { int actual = *ptr; if(actual== 1) *ptr = 0; return actual; } Now i want to use this given instruction...

What is system app [closed]


android,operating-system
I have few questions, What is a system application? How do you go about developing system applications? What is the difference between user and system applications? How is it possible for Android devices to have default system applications without having them installed by the user? ...

shuting down an OS without using API's


c,api,operating-system,shutdown
i was trying to add a shutdown feature to my home-made OS , but i don't really have such API yet . i was wondering how does the OS's terminates the power ? i'd be appreciate it if somebody could give me some guidelines for start .

How to tell the difference between linux and mac


python-3.x,operating-system,cross-platform
I am creating a setup file for a tarball, and I need a way to determine the exact name of the system. I tried os.name, but it returns 'posix' regardless of whether the system is mac or linux. Is there a way to get a more specific OS name?...

Why do we need to install a C++ compiler? [closed]


c++,c,compilation,operating-system
I have heard that all the popular OSes (Windows, Linux, Mac OS X) are built from C++ or C. Why, then, do we need to install GCC or any C++ compiler? Shouldn't programs be compiled by the computer itself as these operating systems support C++ and C?

Automate MySQL backup @localhost with mysqldump in Windows 8


mysql,windows,operating-system,scheduled-tasks,mysqldump
I'm trying to set up a task in Windows 8 to automate my localhost db dump. I've created the task to run daily with the following command line: C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe and parameters: --user=root --password=donttellya --result-file=dumped.sql mydb It works but doesn't terminate (running in the tasks list). Also, I'd...

What is a kernel stack used for?


process,operating-system,stack,kernel,context-switch
The following is a description I read of a context switch between process A and process B. I don't understand what a kernel stack is used for. There is suppose to be a per process kernel stack. And the description I am reading speaks of saving registers of A onto...

Can POSIX named semaphores synchronize threads?


multithreading,synchronization,posix,semaphore
I am looking for a way to synchronize several multi-threaded processes on Linux. C++11-style mutexes only operate among threads within a single process, ans as far as I know, flock doesn't mutually excludes threads of the same process. Therefore, I was thinking of POSIX semaphores. I know that unnamed semaphores...

OS- How to properly print a spacebar in the video memory in a kernel?


c,keyboard,operating-system,keyboard-events
I know this the title is weird (I don't know how to explain it :| )but if u see my problem u will get it. I am making an os in assembly and c i have made a kernel and i have text input and output but i am having...

Bind multiple threads to multiple CPU?


multithreading,process,operating-system,scheduler
I have multiple threads that are accessing the same data and it is too painful to make them thread safe. Therefore, they are now forced to run only on one CPU core using CPU affinity and only one thread can be running at the same time. I was wondering if...

^M behind operating system version?


linux,windows,unix,operating-system
I am currently writing a perl script to extract data from a log file and create statements using the data in a new file. In this new file, one particular data that I've extracted and used here is the operating system's version. It shows Windows XP Service Pack 3^M. I...

Will OS provide virtual address space?


operating-system,virtual-memory
This is an interview question. I am writing all the details the interviewer gave me. The question is:- A system has a memory of 1GB. A process requires only 1MB of memory. Assuming no other processes are running in memory, will the OS provide virtual address translation for this process...

Need suggestions for Android project


android,android-activity,operating-system
I have been assigned one project of Android, but before taking it I need your advice. I have following low level features I need to hide whole operating system in android and only show my application, even device reboot or reset whenever it start it will show only my application....

c - loading raw binaries


c,operating-system,elf
Is it possible to execute a raw binary stored in a char array? I tried doing it like so: #include "stdio.h" int main(int argc, char **argv) { FILE *f = fopen(argv[1],"r"); if(!f) return 1; fseek(f,0,SEEK_END); long l=ftell(f); rewind(f); char *buf = malloc(l+1); fread(buf,1,l,f); fclose(f); void (*func)() = (void(*))buf; func(); }...

Coding of Admin-Guest login section in an OS


linux,authentication,cmd,linux-kernel,operating-system
I developed an authentication algorithm for user authentication as part of my masters thesis and implemented it in PHP. I intend to know how can I implement the algorithm for operating system login, in which language (for linux)? also where the code will reside because it will not be click...

Interrupt Descriptor Table Gate


assembly,x86,operating-system,interrupt-handling,protected-mode
I've written this code to create a sample IDT and load it in to the proper register. I've checked Intel System programming guides for proper structures, and yet I can't get interrupts working. While running kernel code in Bochs, and triggering interrupt (using __asm__ ("int $32"); I get a log...

Possibility of Semaphoreslim.Wait(0) (to prevent multiple execution) causing non execution


c#,asynchronous,async-await,semaphore
The situation I am uncertain of concerns the usage of a "threadsafe" PipeStream where multiple threads can add messages to be written. If there is no queue of messages to be written, the current thread will begin writing to the reading party. If there is a queue, and the queue...

How to detect that music play in background


android,operating-system,music,background-music
I want help in detecting if there is a music play in background on the phone or not because I play more than one music sequentially in my app but they are intersection thank you

Difference between switch & bus architecture?


architecture,operating-system
I was just going through a operating systems textbook but I got stuck at switch architecture . Please explain what it is and how it is different from bus architecture ?...

What does a valid idle thread implementation look like?


operating-system
It is well known that a busy waiting is bad because it wastes CPU time. But what is done in modern operating systems to make the OS idle thread work well? Is it just a busy wait? If so, what is done to conserve power? What is the best approach?...

Trouble outputing file size to a label from a listbox in Python 3


python-3.x,operating-system
I'm using os.path.getsize() to output the size of a file to a label. The file path is stored in a listbox. The function works, but it outputs the file size in bits, so I wrote the following to convert to more appropriate units, and it is now displaying only in...

How my computer knows a char corresponds to which character?


fonts,operating-system
Does my OS load a font file which maps the UTF-8 codes to characters(the way to render it ) when I start up my computer? Since there are a lot of different fonts, the font files will consume a lot of memory? Or the font files are stored in hard-disk...

about GDT and processes


c,assembly,operating-system,protected-mode
how does GDT reflected on linear space of process ( if I understand it right, GDTR contains page and offset ) .? or another version of this question : GDTR is unique for each process .?

C Kernel - Works fine on VM but not actual computer?


c,assembly,operating-system,kernel,hex
I am making a basic C kernel. (Loaded by an assembly program) I am compiling it with an i686-elf cross compiler for windows. My C code is as follows: void cls(); void drawhappy(); void main(){ char *vidptr = (char *)0xb8000; cls(); drawhappy(); } void cls(){ char *vidptr = (char *)0xb8000;...

Using a semaphore [closed]


java,multithreading,semaphore
I would like to know how can i make those thread work one after the other, and how to make the first one always run after the second one. I created a semaphore, but i not sure about how to use it. public class A extends Thread { private int...

Objective-C: Wait to execute 'While' loop until NSURLConnection request is complete


ios,objective-c,nsurlconnection,semaphore,sendasynchronousrequest
Basically I want a way to issue a NSURLRequest multiple times in a loop until a certain condition has been met. I am using a rest api but the rest api only allows up to a maximum of 1,000 results at a time. So if i have, lets say 1,500...

How do I fork an external process in java


java,process,operating-system,fork
I'm trying to fork a new external process (such as Calculator) in Java. I'm new to operating systems but I learned that it's possible using something like : Runtime.getRuntime ().exec ("C:\\Windows\\system32\\calc.exe");. However that doesn't actually fork a new process. Is there anyway I can fork an external process using java?

Protected mode, setting segment registers


assembly,operating-system,gas,protected-mode
I'm recently playing with gnu-assembler in simple os development. I'm using code below to switch CPU to protected mode. In order to do this I set GDT as follow and performed far jump to given label wit 0x08 as GDT offset (to set CS). CPU did not reset itself after...

Why does main function differ from other function from stack point of view


c,assembly,operating-system,stack-trace
I'm studying to use gdb and i got little confused. Here is my code: #include <stdio.h> #include <stdlib.h> #include <string.h> void g(char *password) { } int main(int argc, char *argv[]) { if(argc > 1) { g(argv[1]); } } So with the regular function (g() in this example) if I want...

Not Getting keyboard input in c kernel protected mode


c,operating-system,protected-mode,keyboardinterrupt
I am making an OS in C and 32bit Assembly following James Molloy's tutorial till IRQs and PIT step and i am trying to get keyboard input i tried this code added to the tutorials' code but i am not able to get things right. Keyboard.c: #include "keyboard.h" #include "common.h"...

Determine addresses and page table size


operating-system,size,memory-address,virtual-address-space,page-tables
I know that this question has been asked many times but I still feel I struggle with it. Given: physical memory: 2^20 32-bit system page size: 2^10 I need to determine what a physical and virtual addresses would look like as well as calculate the page table size. I am...

Memory Management (Allocating Pages/Frames to Logical Addresses)


memory,memory-management,operating-system,paging,virtual-memory
I am new to Operating Systems and I was following this handbook which I am reading to get a better grasp on how computers really work. However having stumbled upon Memory Management I found it somewhat confusing when allocating Frames to Pages using Page tables. Say Page/Frame size is 4K...

Random access alternative to named pipes


operating-system,filesystems
Is there a way to create a "file" (i.e. some point in the file system) that can then be opened by any program as a regular file, but reading/writing to it will go to a program instead of to disk? Named pipe seems to meet all the requirements, except it...

How should I simulate sem_wait with a count?


c,posix,semaphore
I'm using semaphore.h and would like to acquire a semaphore if n instead of just one slot is available. Posix does not provide this natively. How can I work around that? I'm bound to using semaphores, no other means of synchronization are possible. I'm pondering using a binary semaphore with...

About Idle tasks in operating systems


operating-system
Every system has IDLE task which will be running if no other tasks are ready to run.So my question is , can IDLE task sleep?. My understanding is if idle task sleep then there is a chance of no task is available for the cpu to run. Is my understanding...

NSUserDefaults not working on Xcode beta with Watch OS2


swift,operating-system,nsuserdefaults,watchkit
I just installed the latest beta of Xcode to try Swift 2 and the improvements made to the Apple Watch development section. I'm actually having an hard time figuring out WHY this basic NSUserDefaults method to share informations between iOS and Watch OS2 isn't working. I followed this step-by-step tutorial...

can paging be slower/faster than swapping on a machine?


memory,operating-system
This is a very naive question, but I wonder if there is any comparison that can be made on paging or swapping speed? They are offering the same functionality (moving from memory to disk, etc) but with different strategies (writing frames, or writing down the whole memory). Now the problem...