[comp.lang.c] Unix and C

richb@railnet.UUCP (Richard Banks) (11/02/90)

How can Unix be written in C ? I thought all OS`s had to be written
in assembly language, else they'd have the same interrupts as the
OS you booted your complier language under to write the program ?

gwyn@smoke.brl.mil (Doug Gwyn) (11/07/90)

In article <LDB1R3w163w@railnet.UUCP> richb@railnet.UUCP (Richard Banks) writes:
>How can Unix be written in C ? I thought all OS`s had to be written
>in assembly language, else they'd have the same interrupts as the
>OS you booted your complier language under to write the program ?

Well, you must have thought wrong...

Seriously, the difference is that between a "hosted" and a "freestanding"
environment, using terminology from the C standard.  Certainly the UNIX
kernel does not expect to simply invoke fseek() and have an underlying
operating system take care of the details.  The details must be programmed
step by step in the UNIX kernel implementation itself.  There are a number
of readily available books that explain how operating systems can be
implemented; there is nothing to prevent the vast majority of such an
implementation from being coded in C.

gunda@motcid.UUCP (Indira Gunda) (11/08/90)

richb@railnet.UUCP (Richard Banks) writes:

>How can Unix be written in C ? I thought all OS`s had to be written
>in assembly language, else they'd have the same interrupts as the
>OS you booted your complier language under to write the program ?

Yes ... Why and how can UNIX be written in C?  I'd also like to know.

_____________________________________________________________________

Indira Gunda				Motorola C.I.D.
uunet!motcid!gunda			1501 W. Shure Drive
					Arlington Hts., IL  60004

fraser@bilby.cs.uwa.oz.au (Fraser Wilson) (11/08/90)

In <4458@mint39.UUCP> gunda@motcid.UUCP (Indira Gunda) writes:

>richb@railnet.UUCP (Richard Banks) writes:

>>How can Unix be written in C ? I thought all OS`s had to be written
>>in assembly language, else they'd have the same interrupts as the
>>OS you booted your complier language under to write the program ?

>Yes ... Why and how can UNIX be written in C?  I'd also like to know.

Why?  Because it makes it very portable.  Any machine that has a C
compiler and a few kernal routines (actually a fair number) can run Unix
fairly easily.  If Unix were written in assembly, it would have to be
completely rewritten for every new computer it runs on.

How?  Well, you have a few system calls to assembly routines, and then
write lots of C to use them.  The C compiler itself is written in C.
The very first one was written with assembly, but every other compiler
(in theory) can then be written in C.  Once you have a C C compiler, it
becomes very easy to port to different machines, just by changing the
target language.

>_____________________________________________________________________

>Indira Gunda				Motorola C.I.D.
>uunet!motcid!gunda			1501 W. Shure Drive
>					Arlington Hts., IL  60004

======================================================================
Fraser Wilson			| Of all the knights in Appledore
fraser@bilby.cs.uwa.oz		| The bravest was Sir Thomas Tom
				| He multiplied as far as four
There can be only one!		| And knew what nine was taken from
	- CM			| To make eleven.  He could write
				| A letter to another knight.	- A.A Milne

epames@eos.ericsson.se (Michael Salmon) (11/08/90)

In article <4458@mint39.UUCP> gunda@motcid.UUCP (Indira Gunda) writes:
>richb@railnet.UUCP (Richard Banks) writes:
>
>>How can Unix be written in C ? I thought all OS`s had to be written
>>in assembly language, else they'd have the same interrupts as the
>>OS you booted your complier language under to write the program ?
>
>Yes ... Why and how can UNIX be written in C?  I'd also like to know.
>
Of course the whole of Unix cannot be written in C but only a small
percentage need be written in assembler. The books on Minix and XINU
are probably the best place to look to see how it's done.

Michael Salmon
L.M. Ericsson
Stockholm

mcdonald@aries.scs.uiuc.edu (Doug McDonald) (11/08/90)

In article <1990Nov8.100816.6004@ericsson.se> epames@eos.ericsson.se writes:
>>
>Of course the whole of Unix cannot be written in C but only a small
>percentage need be written in assembler. 

It is true that an OS cannot be written entirely 100% in conformant 
ANSI C in general. However, for some processors it probably CAN
be written in an UNHOSTED ANSI C with the only "extension" being that
you can be sure that you know exactly what constructs like

*((volatile unsigned char *)12345) = 6789; 

will in fact do (down to the coding level, such as how the mov instruction
would be used. Perhaps a couple of pragmas would help.

Some PDP-11 processors are probably in this class. I can think of only one
obvious problem - many have a Processor Status register that requires
special instructions to access.

Processors that have special IO instructions of course exist - there
indeed C alone cannot an operating system create.


Doug McDonald

boyd@necisa.ho.necisa.oz (Boyd Roberts) (11/09/90)

In article <4458@mint39.UUCP> gunda@motcid.UUCP (Indira Gunda) writes:
>
>Yes ... Why and how can UNIX be written in C?  I'd also like to know.
>

Ever hear of the Church-Turing thesis?


Boyd Roberts			boyd@necisa.ho.necisa.oz.au

``When the going gets wierd, the weird turn pro...''

siva@bally.Bally.COM (Siva Chelliah) (11/09/90)

In article <fraser.658038441@bilby> fraser@bilby.cs.uwa.oz.au (Fraser Wilson) writes:
>In <4458@mint39.UUCP> gunda@motcid.UUCP (Indira Gunda) writes:
>
>>richb@railnet.UUCP (Richard Banks) writes:
>
>>>How can Unix be written in C ? I thought all OS`s had to be written
>>>in assembly language, else they'd have the same interrupts as the
>>>OS you booted your complier language under to write the program ?
>
>>Yes ... Why and how can UNIX be written in C?  I'd also like to know.
>
>Why?  Because it makes it very portable.  Any machine that has a C
>compiler and a few kernal routines (actually a fair number) can run Unix
>fairly easily.  If Unix were written in assembly, it would have to be
>completely rewritten for every new computer it runs on.

This discussion reminds me of something.

Q: How is the first compiler is written ? 
A: In assembly language.

Q: How is the first assembler is written ?
A: In machine language.

Q: If you have just the hardware, how would you feed the machine language
   into the computer ?
A: I thing you should burn the machine language into a chip, that would 
   accept a machine language from the user, and execute it. 


>
>How?  Well, you have a few system calls to assembly routines, and then
>write lots of C to use them.  The C compiler itself is written in C.
>

Assume you have a C compiler ( write it any way you want - assembly, or Pascal
or anything ).  It can be as sloppy as you want.  
Let's call it  sloppy_comp_sloppy.exe

sloppy_comm_sloppy.exe ===> a C compiler which is sloppy(doesn't do any 
                            optimization)

Now write a optimizing C compiler in C call it opti_comp.c

opti_comp.c   ===> a C compiler that will optimize other C programs.

compile this with sloppy_comm_sloppy.exe , and you will get

opti_comp_sloppy.exe  ==> it is a sloppy exe file (will run slow, 
                          but will compile other C programs and produce
                          good exe programs)

Now, compile opti_comp.c using opti_comp_sloppy.exe, and you will get

opti_comp_opti.exe  ===> an optimized exe file which will produce other good
                         exe programs.

So we started with a sloppy C compiler and using that we produced a 
optimizing C compiler ( which itself is optimized).

Siva
siva@bally.bally.com

dylan@ibmpcug.co.uk (Matthew Farwell) (11/10/90)

In article <1929@necisa.ho.necisa.oz> boyd@necisa.ho.necisa.oz (Boyd Roberts) writes:
>In article <4458@mint39.UUCP> gunda@motcid.UUCP (Indira Gunda) writes:
>>Yes ... Why and how can UNIX be written in C?  I'd also like to know.
>Ever hear of the Church-Turing thesis?

No. Tell us about it Unkle Boyd...

Dylan.
-- 
Matthew J Farwell                 | Email: dylan@ibmpcug.co.uk
The IBM PC User Group, PO Box 360,|        ...!uunet!ukc!ibmpcug!dylan
Harrow HA1 4LQ England            | CONNECT - Usenet Access in the UK!!
Phone: +44 81-863-1191            | Sun? Don't they make coffee machines?

dbc@bushido.uucp (Dave Caswell) (11/10/90)

.>>>How can Unix be written in C ? I thought all OS`s had to be written
.>>>in assembly language, else they'd have the same interrupts as the
.>>>OS you booted your complier language under to write the program ?
.>
.>>Yes ... Why and how can UNIX be written in C?  I'd also like to know.
.>
.>Why?  Because it makes it very portable.  Any machine that has a C
.>compiler and a few kernal routines (actually a fair number) can run Unix
.>fairly easily.  If Unix were written in assembly, it would have to be
.>completely rewritten for every new computer it runs on.

Unless of course it was written in 386 assembler.  It's portable.

-- 
David Caswell                             dbc%bushido.uucp@umich.edu

steve@taumet.com (Stephen Clamage) (11/11/90)

boyd@necisa.ho.necisa.oz (Boyd Roberts) writes:

>>Yes ... Why and how can UNIX be written in C?  I'd also like to know.

>Ever hear of the Church-Turing thesis?

Yes, and it is not relevant in the least.  C-T says (in effect) that
a general-purpose computer can do anything a special-purpose computer
can do.  This has nothing whatever to do with writing a specific program
in a specific language.
-- 

Steve Clamage, TauMetric Corp, steve@taumet.com

zane@ddsw1.MCS.COM (Sameer Parekh) (11/11/90)

Church-Turing thesis:
	I haven't heard of it. What is it?

-- 
zane@ddsw1.MCS.COM

 
                                   

gwyn@smoke.brl.mil (Doug Gwyn) (11/11/90)

In article <1990Nov10.124655.797@bushido.uucp> dbc@bushido.uucp (Dave Caswell) writes:
>Unless of course it was written in 386 assembler.  It's portable.

If you don't know what words like "portable" mean, you should keep quiet.
None of the dozens of computers around here has a 386 assembler.

floyd@hayes.ims.alaska.edu (Floyd Davidson) (11/11/90)

In article <1990Nov10.124655.797@bushido.uucp> dbc@bushido.uucp (Dave Caswell) writes:
>.>>>How can Unix be written in C ? I thought all OS`s had to be written
>.> [ ... ]
>.>Why?  Because it makes it very portable.  Any machine that has a C
>.>compiler and a few kernal routines (actually a fair number) can run Unix
>.>fairly easily.  If Unix were written in assembly, it would have to be
>.>completely rewritten for every new computer it runs on.
>
>Unless of course it was written in 386 assembler.  It's portable.

To my 68020?


-- 
Floyd L. Davidson   floyd@hayes.ims.alaska.edu    floydd@chinet.chi.il.us
Salcha, AK 99714    connected by paycheck to Alascom, Inc.
When *I* speak for them, one of us will be *out* of business in a hurry.

kdq@demott.COM (Kevin D. Quitt) (11/12/90)

In article <1990Nov11.072404.7312@hayes.ims.alaska.edu> floyd@hayes.ims.alaska.edu (Floyd Davidson) writes:
>In article <1990Nov10.124655.797@bushido.uucp> dbc@bushido.uucp (Dave Caswell) writes:
>>
>>Unless of course it was written in 386 assembler.  It's portable.
>
>To my 68020?

    Sure - you just need a 386 machine-instruction interpreter.  Not the
programmer's fault if you don't have one.  Of course, it'll probably run
as fast.  8-{)}

-- 
 _
Kevin D. Quitt         demott!kdq   kdq@demott.com
DeMott Electronics Co. 14707 Keswick St.   Van Nuys, CA 91405-1266
VOICE (818) 988-4975   FAX (818) 997-1190  MODEM (818) 997-4496 PEP last

                96.37% of all statistics are made up.

dhesi%cirrusl@oliveb.ATC.olivetti.com (Rahul Dhesi) (11/12/90)

>Yes ... Why and how can UNIX be written in C?  I'd also like to know.

Given that one can write vi macros that create a Turing machine, it
should be possible to rewrite UNIX in vi.  Once you have UNIX in vi
(complete with all utilities), you can write the C compiler afresh, and
then write UNIX using that C compiler.  Once you have done so, you
won't need the vi macros any more, so you can then go back to using
Emacs using which it is not possible to write UNIX (because Emacs takes
up all memory, so there's none left for UNIX).

The significance of the above is left as an exercise for the reader.
--
Rahul Dhesi <dhesi%cirrusl@oliveb.ATC.olivetti.com>
UUCP:  oliveb!cirrusl!dhesi

lerman@stpstn.UUCP (Ken Lerman) (11/12/90)

In article <1929@necisa.ho.necisa.oz> boyd@necisa.ho.necisa.oz (Boyd Roberts) writes:
->In article <4458@mint39.UUCP> gunda@motcid.UUCP (Indira Gunda) writes:
->>
->>Yes ... Why and how can UNIX be written in C?  I'd also like to know.
->>
->
->Ever hear of the Church-Turing thesis?
->
->
->Boyd Roberts			boyd@necisa.ho.necisa.oz.au
->
->``When the going gets wierd, the weird turn pro...''

Isn't that something about a guided tour of Italy never runs out of churches?

Ken

gwyn@smoke.brl.mil (Doug Gwyn) (11/12/90)

In article <2679@cirrusl.UUCP> dhesi%cirrusl@oliveb.ATC.olivetti.com (Rahul Dhesi) writes:
>Given that one can write vi macros that create a Turing machine, ...

That is certainly NOT given.  In fact, it's obvious that a Turing machine
cannot be implemented at all.

>... it should be possible to rewrite UNIX in vi.

Also not true, because UNIX has some performance constraints that any
implementation in "vi" command language would be unable to meet.

martin@mwtech.UUCP (Martin Weitzel) (11/15/90)

In article <1990Nov8.152413.10792@ux1.cso.uiuc.edu: mcdonald@aries.scs.uiuc.edu (Doug McDonald) writes:
:In article <1990Nov8.100816.6004@ericsson.se> epames@eos.ericsson.se writes:
:>>
:>Of course the whole of Unix cannot be written in C but only a small
:>percentage need be written in assembler. 
:
:It is true that an OS cannot be written entirely 100% in conformant 
:ANSI C in general. However, for some processors it probably CAN
:be written in an UNHOSTED ANSI C with the only "extension" being that
:you can be sure that you know exactly what constructs like
:
:*((volatile unsigned char *)12345) = 6789; 
:
:will in fact do (down to the coding level, such as how the mov instruction
:would be used. Perhaps a couple of pragmas would help.
:
:Some PDP-11 processors are probably in this class. I can think of only one
:obvious problem - many have a Processor Status register that requires
:special instructions to access.
:
:Processors that have special IO instructions of course exist - there
:indeed C alone cannot an operating system create.

Given that C-Compilers are allowed (though, of course, not required!)
to give `undefined behaviour' some implementation defined meaning,
you can even go some step further: An implementation that defines the
mapping functions between data adresses and function pointers in a
resonable way, and with a machine, that allows to execute instructions
from the data space, you can put bytes of machine instructions in a
statically initialized array% and so "execute" arbitrary op-codes,
like the following example shows:

	/* WARNING: HIGLY UNPORTABLE CODE AHEAD */

	static char machprog[] = { ... , ... , ... };
	                          ^^^^^^^^^^^^^^^^^   specify machine
	                                              instructions here

	(*(void (*)()) &machprog)();                  and 'jump subroutine'
	                                              to them here

Of course, this technique is only recommendable in certain special
cases. A cleaner approach were a separate module in assembly language.
Nevertheless, one part of the `C-Philosophy' allways was and still is:

    Don't prevent the programmer from doing what needs to be done!

%: The example shows an array of char here; depending on the machine
   an int or long array could be more appropriate.
-- 
Martin Weitzel, email: martin@mwtech.UUCP, voice: 49-(0)6151-6 56 83

paul@tetrauk.UUCP (Paul Ashton) (11/17/90)

martin@mwtcho.UUCP wrote:
> Given that C-Compilers are allowed (though, of course, not required!)
> to give `undefined behaviour' some implementation defined meaning,
> ...
Surely the simplest ANSI C program to produce Unix is:-

#pragma PRODUCE_A_UNIX_IMPLEMENTATION

No messy machine code or anything.

All you need to do is find a compiler that implements it :-)
-- 
Paul

ted@oz.plymouth.edu (The Wiz) (11/20/90)

In article <LDB1R3w163w@railnet.UUCP> richb@railnet.UUCP (Richard Banks) writes:
>How can Unix be written in C ? I thought all OS`s had to be written
>in assembly language, else they'd have the same interrupts as the
>OS you booted your complier language under to write the program ?

	Have you ever heard of cross compilation?  This is where a compiler
on another machine is directed (out of lack for a better word) to produce
machine "object" code for another machine.  I think you are confused about
the difference between assembly lang. and machine language.  Most good
books on Operating systems Explain how it is done, you might want to look
at one.  How long do you think it would to take to write a large 
operating system in machine code?  Hint: A long time.  Unix is based
on the idea that when you port it to a new machine you only need to re-write
the kernel (The lowest level) of the OS,  thus making it easily portable.

				--> Ted

igb@fulcrum.bt.co.uk (Ian G Batten) (11/20/90)

ted@oz.plymouth.edu (The Wiz) writes:
> operating system in machine code?  Hint: A long time.  Unix is based
> on the idea that when you port it to a new machine you only need to re-write
> the kernel (The lowest level) of the OS,  thus making it easily portable.

In fact, you re-write a great deal less than that.  Porting a kernel to
a new machine is nothing like writing a new kernel.

ian