12 Replies - 935 Views - Last Post: 26 June 2018 - 02:17 PM

#1 rnty   User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 280
  • Joined: 14-August 12

Allow only one instance of a program

Posted 26 June 2018 - 03:21 AM

Hello,

I was asked the following question in an interview

Write a program to print from one to one crore such that if any other instance of the program is executed when the processing is in progress a message like "Program busy" should be displayed. It should be done without using any system specific functionalities and should be language independent.

Kindly suggest solutions.

Thank You
Is This A Good Question/Topic? 0
  • +

Replies To: Allow only one instance of a program

#2 andrewsw   User is online

  • head thrashing
  • member icon

Reputation: 6667
  • View blog
  • Posts: 27,299
  • Joined: 12-December 12

Re: Allow only one instance of a program

Posted 26 June 2018 - 03:25 AM

Is this the interview question that you have been assigned? What have you considered?
Was This Post Helpful? 0
  • +
  • -

#3 rnty   User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 280
  • Joined: 14-August 12

Re: Allow only one instance of a program

Posted 26 June 2018 - 03:35 AM

Not assigned. The interview is done. Didn't get selected. I considered a file at a common location that gets created once the program is initialized and deleted once the printing is done. So any instance that start's up checks if the file is there. If the file is present that means that the program is executing else not.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7361
  • View blog
  • Posts: 15,284
  • Joined: 16-October 07

Re: Allow only one instance of a program

Posted 26 June 2018 - 04:16 AM

Given your requirement for system agnosticism, you basically have it: use a file on the file system as a flag. This can be closely related to file locking or simply that a file exists. On some systems, you'll see the extant file containing a process id, which can be used to kill the running process if desired (e.g. vncserver .)

OS specific solutions are more nuanced, as the OS can usually locate a process that launched a particular executable with little trouble.
Was This Post Helpful? 0
  • +
  • -

#5 rnty   User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 280
  • Joined: 14-August 12

Re: Allow only one instance of a program

Posted 26 June 2018 - 04:46 AM

OS specific solutions are disallowed
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6657
  • View blog
  • Posts: 22,740
  • Joined: 05-May 12

Re: Allow only one instance of a program

Posted 26 June 2018 - 05:29 AM

How will that work for Javascript running in a sandbox (e.g Web Browser) where it doesn't have file system access?

How will that work for Logo which (traditionally) doesn't have any file I/O?
Was This Post Helpful? 0
  • +
  • -

#7 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7361
  • View blog
  • Posts: 15,284
  • Joined: 16-October 07

Re: Allow only one instance of a program

Posted 26 June 2018 - 07:45 AM

@Skydiver: you seem to be very broadly defining "program," given that a "program instance" is explicitly mentioned. :P But, for fun...

View PostSkydiver, on 26 June 2018 - 07:29 AM, said:

How will that work for Javascript running in a sandbox (e.g Web Browser) where it doesn't have file system access?

Isn't your browser page a single instance? How do you imagine multiple "programs" run here?

View PostSkydiver, on 26 June 2018 - 07:29 AM, said:

How will that work for Logo which (traditionally) doesn't have any file I/O?

Logo is interpreted: the "program instance" is the instruction set currently running and you only get one.

Now, if you wanted to call "threads" programs you might be able to find interesting use cases, but you'd also be moving very far away from the "language independent" criteria.
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6657
  • View blog
  • Posts: 22,740
  • Joined: 05-May 12

Re: Allow only one instance of a program

Posted 26 June 2018 - 10:13 AM

View Postbaavgai, on 26 June 2018 - 10:45 AM, said:

Isn't your browser page a single instance? How do you imagine multiple "programs" run here?

Ex. Page has two different sections for ads. If the "program" is an ad, how to let one display, while have the other display "Program busy."

View Postbaavgai, on 26 June 2018 - 10:45 AM, said:

Logo is interpreted: the "program instance" is the instruction set currently running and you only get one.

BASIC is also interpreted. I could have two instances of the BASIC interpreter running my program, or two instances of the LOGO window running.
Was This Post Helpful? 0
  • +
  • -

#9 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7361
  • View blog
  • Posts: 15,284
  • Joined: 16-October 07

Re: Allow only one instance of a program

Posted 26 June 2018 - 10:55 AM

View PostSkydiver, on 26 June 2018 - 12:13 PM, said:

Page has two different sections for ads.

Each of those sections is it's own sub page. It's own context. Two be another "program" instance, it would need exist in the same context.

View PostSkydiver, on 26 June 2018 - 12:13 PM, said:

BASIC is also interpreted. I could have two instances of the BASIC interpreter running my program, or two instances of the LOGO window running.

Indeed. BASIC is your program. LOGO is your program. Having two instances of those programs is the issue in question, not the state of said programs: i.e. the code currently being interpreted.
Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11374
  • View blog
  • Posts: 19,406
  • Joined: 19-March 11

Re: Allow only one instance of a program

Posted 26 June 2018 - 11:20 AM

Surely the interpreted/compiled distinction doesn't matter there if you're using a flag file. In any case the code that's running is going to exhibit the same behavior: it'll look for the flag, and if it doesn't find the flag it'll set the flag and run, and hopefully it'll clear the flag when it goes to terminate.
If the language can't talk to the filesystem, then yeah this is sort of harder to do, but whether it's interpreted seems irrelevant to the original problem.
Was This Post Helpful? 0
  • +
  • -

#11 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7361
  • View blog
  • Posts: 15,284
  • Joined: 16-October 07

Re: Allow only one instance of a program

Posted 26 June 2018 - 11:38 AM

View Postjon.kiparsky, on 26 June 2018 - 01:20 PM, said:

Surely the interpreted/compiled distinction doesn't matter there if you're using a flag file.

Agreed. Mostly thinking sandboxes here. If the program can behave in the general purpose domain then such a distinction is meaningless.

Admittedly, the BASIC threw me, because then I'm thinking of running the thing and on a modern OS it would, indeed, be sandboxed. I believe there are a few Javascript implementations of GW-BASIC. I've considered writing one myself.
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6657
  • View blog
  • Posts: 22,740
  • Joined: 05-May 12

Re: Allow only one instance of a program

Posted 26 June 2018 - 02:10 PM

View Postbaavgai, on 26 June 2018 - 01:55 PM, said:

View PostSkydiver, on 26 June 2018 - 12:13 PM, said:

Page has two different sections for ads.

Each of those sections is it's own sub page. It's own context. Two be another "program" instance, it would need exist in the same context.

That would only be true if each ad section was in its own truly isolated section (like an iframe). In the early days of web page ads, I've seen the script and/or CSS of one ad accidentally impacting the behavior of the other ad depending on which ad was loaded earlier in the page. If they can impact each other, that would mean they are in the same context because of the lack of isolation.
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6657
  • View blog
  • Posts: 22,740
  • Joined: 05-May 12

Re: Allow only one instance of a program

Posted 26 June 2018 - 02:17 PM

Or to get rid of angle where context for each "program" should be isolated, consider the Redcode programming language intended to run within the MARS enviroment for Core Wars. The two "programs" are intended to duke it out within the same context.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1